日韩在线不卡一区二区三区四区五区,欧美视频一区二区三,欧美视频中文字幕一区二区,高端 精品 国产 探花,亚欧无吗一二三四五六区
RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
java聊天小程序源代碼 java簡單聊天程序代碼

誰能給個JAVA的小程序代碼,越小越好!

這是我曉得的最簡單的java小程序代碼了你可以看看:

成都創(chuàng)新互聯(lián)為您提適合企業(yè)的網(wǎng)站設(shè)計?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強的網(wǎng)絡(luò)競爭力!結(jié)合企業(yè)自身,進行網(wǎng)站設(shè)計及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè), 我們的網(wǎng)頁設(shè)計師為您提供的解決方案。

package com.kenki.emp;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

import java.sql.SQLException;

import java.sql.*;

public class emp extends HttpServlet {

private static final String CONTENT_TYPE = "text/html; charset=GBK";

//Initialize global variables

public void init() throws ServletException {

}

//Process the HTTP Get request

public void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

response.setContentType(CONTENT_TYPE);

PrintWriter out = response.getWriter();

String code = request.getParameter("code");

String name = request.getParameter("name");

String pay = request.getParameter("pay");

System.out.println("empcode:" + code);

System.out.println("name:" + name);

System.out.println("pay:" + pay);

//創(chuàng)建驅(qū)動

new com.microsoft.jdbc.sqlserver.SQLServerDriver();

String strd =

"jdbc:microsoft:sqlserver://localhost:1433;databasename=emp_dates";

String username = "sa";

String pws = "";

try {

java.sql.Connection conn = java.sql.DriverManager.getConnection(

strd, username, pws);

String strs = "insert into emp values(?,?,?)";

java.sql.PreparedStatement pre = conn.prepareStatement(strs);

pre.setString(1, code);

pre.setString(2, name);

pre.setString(3, pay);

pre.execute();

pre.close();

conn.close();

//重定向至查詢頁面

out.println("成功保存!!");

response.sendRedirect("emp.html");

} catch (SQLException ss) {

ss.printStackTrace();

response.sendRedirect("/WebModule1/error.html");

}

}

//Process the HTTP Post request

public void doPost(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

doGet(request, response);

}

//Clean up resources

public void destroy() {

}

}

有沒有人能提供一個java寫的socket聊天室小程序

服務(wù)器端:

import java.io.*;

import java點虐 .*;

public class TestServer{

public static void main(String args[]){

try{

ServerSocket server = null;

try{

server = new ServerSocket(3456);

}catch(Exception e){

System.out.println("can not listen to:" + e);

}

Socket socket = null;

try{

socket = server.accept();

}catch(Exception e){

System.out.println("Error:" + e);

}

String line;

BufferedReader is = new BufferedReader(new InputStreamReader(

socket.getInputStream()));

PrintWriter os = new PrintWriter(socket.getOutputStream());

BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Client:" + is.readLine());

line = sin.readLine();

while (!line.equals("bye")){

os.println(line);

os.flush();

System.out.println("Server:" + line);

System.out.println("Client:" + is.readLine());

line = sin.readLine();

}

is.close();

os.close();

socket.close();

server.close();

}catch(Exception e){

System.out.println("Error" + e);

}

}

}

客戶端:

import java.io.*;

import java點虐 .*;

public class TestClient{

public static void main(String args[]){

try{

Socket socket = new Socket("127.0.0.1",3456);

BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

PrintWriter os = new PrintWriter(socket.getOutputStream());

BufferedReader is = new BufferedReader(new InputStreamReader(

socket.getInputStream()));

String readline;

readline = sin.readLine();

while (!readline.equals("bye")){

os.println(readline);

os.flush();

System.out.println("Client:" + readline);

System.out.println("Server:" + is.readLine());

readline = sin.readLine();

}

os.close();

is.close();

socket.close();

}catch(Exception e){

System.out.println("Error" + e);

}

}

}

要么再看看這個

這個應(yīng)該可以的拉:

import java.applet.*;

import java.awt.*;

import java.io.*;

import java點虐 .*;

import java.awt.event.*;

public class ChatClient extends Applet{

protected boolean loggedIn;//登入狀態(tài)

protected Frame cp;//聊天室框架

protected static int PORTNUM=7777; //缺省端口號7777

protected int port;//實際端口號

protected Socket sock;

protected BufferedReader is;//用于從sock讀取數(shù)據(jù)的BufferedReader

protected PrintWriter pw;//用于向sock寫入數(shù)據(jù)的PrintWriter

protected TextField tf;//用于輸入的TextField

protected TextArea ta;//用于顯示對話的TextArea

protected Button lib;//登入按鈕

protected Button lob;//登出的按鈕

final static String TITLE ="Chatroom applet";

protected String paintMessage;//發(fā)表的消息

public ChatParameter Chat;

public void init(){

paintMessage="正在生成聊天窗口";

repaint();

cp=new Frame(TITLE);

cp.setLayout(new BorderLayout());

String portNum=getParameter("port");//呢個參數(shù)勿太明

port=PORTNUM;

if (portNum!=null) //書上是portNum==null,十分有問題

port=Integer.parseInt(portNum);

//CGI

ta=new TextArea(14,80);

ta.setEditable(false);//read only attribute

ta.setFont(new Font("Monospaced",Font.PLAIN,11));

cp.add(BorderLayout.NORTH,ta);

Panel p=new Panel();

Button b;

//for login button

p.add(lib=new Button("Login"));

lib.setEnabled(true);

lib.requestFocus();

lib.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e){

login();

lib.setEnabled(false);

lob.setEnabled(true);

tf.requestFocus();//將鍵盤輸入鎖定再右邊的文本框中

}

});

//for logout button

p.add(lob=new Button ("Logout"));

lob.setEnabled(false);

lob.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

logout();

lib.setEnabled(true);

lob.setEnabled(false);

lib.requestFocus();

}

});

p.add(new Label ("輸入消息:"));

tf=new TextField(40);

tf.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

if(loggedIn){

//pw.println(Chat.CMD_BCAST+tf.getText());//Chat.CMD....是咩野來?

int j=tf.getText().indexOf(":");

if(j0) pw.println(Chat.CMD_MESG+tf.getText());

else

pw.println(Chat.CMD_BCAST+tf.getText());

tf.setText("");//勿使用flush()?

}

}

});

p.add(tf);

cp.add(BorderLayout.SOUTH,p);

cp.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

//如果執(zhí)行了setVisible或者dispose,關(guān)閉窗口

ChatClient.this.cp.setVisible(false);

ChatClient.this.cp.dispose();

logout();

}

});

cp.pack();//勿明白有咩用?

//將Frame cp放在中間

Dimension us=cp.getSize(),

them=Toolkit.getDefaultToolkit().getScreenSize();

int newX=(them.width-us.width)/2;

int newY=(them.height-us.height)/2;

cp.setLocation(newX,newY);

cp.setVisible(true);

paintMessage="Window should now be visible";

repaint();

}

//登錄聊天室

public void login(){

if(loggedIn) return;

try{

sock=new Socket(getCodeBase().getHost(),port);

is=new BufferedReader(new InputStreamReader(sock.getInputStream()));

pw=new PrintWriter(sock.getOutputStream(),true);

}catch(IOException e){

showStatus("Can't get socket: "+e);

cp.add(new Label("Can't get socket: "+e));

return;}

//構(gòu)造并且啟動讀入器,從服務(wù)器讀取數(shù)據(jù),輸出到文本框中

//這里,長成一個線程來避免鎖住資源(lockups)

new Thread (new Runnable(){

public void run(){

String line;

try{

while(loggedIn ((line=is.readLine())!=null))

ta.appendText(line+"\n");

}catch(IOException e){

showStatus("我的天啊,掉線了也!!!!");

return;

}

}

}).start();

//假定登錄(其實只是打印相關(guān)信息,并沒有真正登錄)

// pw.println(Chat.CMD_LOGIN+"AppletUser");

pw.println(Chat.CMD_LOGIN+"AppletUser");

loggedIn =true;

}

//模仿退出的代碼

public void logout(){

if(!loggedIn)

return;

loggedIn=false;

try{

if(sock!=null)

sock.close();

}catch(IOException ign){

// 異常處理哦

}

}

//沒有設(shè)置stop的方法,即使從瀏覽器跳到另外一個網(wǎng)頁的時候

//聊天程序還可以繼續(xù)運行

public void paint(Graphics g){

Dimension d=getSize();

int h=d.height;

int w=d.width;

g.fillRect(0,0,w,2);

g.setColor(Color.black);

g.drawString(paintMessage,10,(h/2)-5);

}

}

聊天室服務(wù)器端

import java點虐 .*;

import java.io.*;

import java.util.*;

public class ChatServer{

//聊天室管理員ID

protected final static String CHATMASTER_ID="ChatMaster";

//系統(tǒng)信息的分隔符

protected final static String SEP=": ";

//服務(wù)器的Socket

protected ServerSocket servSock;

//當(dāng)前客戶端列表

protected ArrayList clients;

//調(diào)試標(biāo)記

protected boolean DEBUG=false;

public ChatParameter Chat;

//主方法構(gòu)造一個ChatServer,沒有返回值

public static void main(String[] argv){

System.out.println("Chat server0.1 starting");

ChatServer w=new ChatServer();

w.runServer();

System.out.println("***ERROR*** Chat server0.1 quitting");

}

//構(gòu)造和運行一個聊天服務(wù)

ChatServer(){

Chat=new ChatParameter();

clients=new ArrayList();

try{

servSock=new ServerSocket(7777);//實有問題拉,不過可能是他自己定義既一個class.

System.out.println("Chat Server0.1 listening on port:"+7777);

}catch(Exception e){

log("IO Exception in ChatServer.init");

System.exit(0);

}

}

public void runServer(){

try{

while(true){

Socket us=servSock.accept();

String hostName=us.getInetAddress().getHostName();

System.out.println("Accpeted from "+hostName);

//一個處理的線程

ChatHandler cl=new ChatHandler(us,hostName);

synchronized(clients){

clients.add(cl);

cl.start();

if(clients.size()==1)

cl.send(CHATMASTER_ID,"Welcome!You are the first one here");

else{

cl.send(CHATMASTER_ID,"Welcome!You are the latest of"+

clients.size()+" users.");

}

}

}

}catch(Exception e){

log("IO Exception in runServer:"+e);

System.exit(0);

}

}

protected void log(String s){

System.out.println(s);

}

//處理會話的內(nèi)部的類

protected class ChatHandler extends Thread {

//客戶端scoket

protected Socket clientSock;

//讀取socket的BufferedReader

protected BufferedReader is ;

//在socket 上發(fā)送信息行的PrintWriter

protected PrintWriter pw;

//客戶端出主機

protected String clientIP;

//句柄

protected String login;

public ChatHandler (Socket sock,String clnt)throws IOException {

clientSock=sock;

clientIP=clnt;

is=new BufferedReader(

new InputStreamReader(sock.getInputStream()));

pw=new PrintWriter (sock.getOutputStream(),true);

}

//每一個ChatHandler是一個線程,下面的是他的run()方法

//用于處理會話

public void run(){

String line;

try{

while((line=is.readLine())!=null){

char c=line.charAt(0);//我頂你老母啊 ,果只Chat.CMD咩xx冇定義 撲啊///!!!

line=line.substring(1);

switch(c){

//case Chat.CMD_LOGIN:

case 'l':

if(!Chat.isValidLoginName(line)){

send(CHATMASTER_ID,"LOGIN"+line+"invalid");

log("LOGIN INVALID from:"+clientIP);

continue;

}

login=line;

broadcast(CHATMASTER_ID,login+" joins us,for a total of"+

clients.size()+" users");

break;

// case Chat.CMD_MESG:

case 'm':

if(login==null){

send(CHATMASTER_ID,"please login first");

continue;

}

int where =line.indexOf(Chat.SEPARATOR);

String recip=line.substring(0,where);

String mesg=line.substring (where+1);

log("MESG: "+login+"---"+recip+": "+mesg);

ChatHandler cl=lookup(recip);

if(cl==null)

psend(CHATMASTER_ID,recip+"not logged in.");

else

cl.psend(login,mesg);

break;

//case Chat.CMD_QUIT:

case 'q':

broadcast(CHATMASTER_ID,"Goodbye to "+login+"@"+clientIP);

close();

return;//ChatHandler結(jié)束

// case Chat.CMD_BCAST:

case 'b':

if(login!=null)

broadcast(login,line);

else

log("BL FROM"+clientIP);

break;

default:

log("Unknow cmd"+c+"from"+login+"@"+clientIP);

}

}

}catch(IOException e){

log("IO Exception :"+e);

}finally{

//sock 結(jié)束,我們完成了

//還不能發(fā)送再見的消息

//得有簡單的基于命令的協(xié)議才行

System.out.println(login+SEP+"All Done");

synchronized(clients){

clients.remove(this);

if(clients.size()==0){

System.out.println(CHATMASTER_ID+SEP+

"I'm so lonely I could cry");

}else if(clients.size()==1){

ChatHandler last=(ChatHandler)clients.get(0);

last.send(CHATMASTER_ID,"Hey,you are talking to yourself again");

}

else{

broadcast(CHATMASTER_ID,"There are now"+clients.size()+" users");

}

}

}

}

protected void close(){

if(clientSock==null){

log("close when not open");

return;

}

try{

clientSock.close();

clientSock=null;

}catch(IOException e){

log("Failure during close to "+clientIP);

}

}

//發(fā)送一條消息給用戶

public void send(String sender,String mesg){

pw.println(sender+SEP+"*"+mesg);

}

//發(fā)送私有的消息

protected void psend(String sender ,String msg){

send("*"+sender+"*",msg);

}

//發(fā)送一條消息給所有的用戶

public void broadcast (String sender,String mesg){

System.out.println("Broadcasting"+sender+SEP+mesg);

for(int i=0;iclients.size();i++){

ChatHandler sib=(ChatHandler)clients.get(i);

if(DEBUG)

System.out.println("Sending to"+sib);

sib.send(sender,mesg);

}

if(DEBUG) System.out.println("Done broadcast");

}

protected ChatHandler lookup(String nick){

synchronized(clients){

for(int i=0;iclients.size();i++){

ChatHandler cl=(ChatHandler)clients.get(i);

if(cl.login.equals(nick))

return cl;

}

}

return null;

}

//將ChatHandler對象轉(zhuǎn)換成一個字符串

public String toString(){

return "ChatHandler["+login+"]";

}

}

}

public class ChatParameter {

public static final char CMD_BCAST='b';

public static final char CMD_LOGIN='l';

public static final char CMD_MESG='m';

public static final char CMD_QUIT='q';

public static final char SEPARATOR=':';//?????

public static final int PORTNUM=7777;

public boolean isValidLoginName(String line){

if (line.equals("CHATMASTER_ID"))

return false;

return true;

}

public void main(String[] argv){

}

}

如何用java做一個聊天小程序 要求使用圖形用戶界面,可以實現(xiàn)一個聊天室中多人聊天,也可以兩人私聊,

給你一個簡單的實現(xiàn)吧,注意一定要先運行MyServer.java

//MyCilent.java

import java.io.*;

import java點虐 .*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MyClient extends JFrame implements ActionListener{

JTextField tf;

JTextArea tx;

JButton bt;

PrintWriter out;

public MyClient(){

tf=new JTextField(20);

tx=new JTextArea();

tx.setLineWrap(true);

tx.setWrapStyleWord(true);

JPanel pan=new JPanel();

JScrollPane jsp=new JScrollPane(tx);

add(jsp,"Center");

bt=new JButton("SEND");

bt.addActionListener(this);

pan.add(tf);

pan.add(bt);

add(pan,"South");

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

setTitle("THE CLIENT");

setSize(400,300);

setVisible(true);

try{

Socket socket=new Socket("127.0.0.1",1680);

out=new PrintWriter(socket.getOutputStream(),true);

InputStreamReader in = new InputStreamReader(socket.getInputStream());

BufferedReader sin=new BufferedReader(in);

String s;

while(true){

s=sin.readLine();

tx.append("#Server Said#: "+s+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

tx.append("@Client Said@: "+tf.getText()+"\n");

out.println(tf.getText());

tf.setText("");

}

}

public static void main(String[] args){

MyClient mct = new MyClient();

}

}

//MyServer.java

import java.io.*;

import java點虐 .*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MyServer extends JFrame implements ActionListener{

JTextField tf;

JTextArea tx;

JButton bt;

JScrollPane jsp;

JPanel pan;

PrintWriter out;

public MyServer(){

tx=new JTextArea();

tx.setLineWrap(true);

tx.setWrapStyleWord(true);

jsp=new JScrollPane(tx);

tf=new JTextField(20);

bt=new JButton("SEND");

bt.addActionListener(this);

pan=new JPanel();

pan.add(tf);

pan.add(bt);

add(pan,"South");

add(jsp,"Center");

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

setTitle("THE SERVER");

setSize(400,300);

setVisible(true);

try{

ServerSocket server = new ServerSocket(1680);

Socket socket = server.accept();

InputStreamReader in = new InputStreamReader(socket.getInputStream());

BufferedReader sin=new BufferedReader(in);

out=new PrintWriter(socket.getOutputStream(),true);

while(true){

String s=sin.readLine();

tx.append("@Client Said@: "+s+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

String st = tf.getText();

tx.append("#Server Said#: "+st+"\n");

out.println(st);

tf.setText("");

}

}

public static void main(String[] args){

MyServer msr = new MyServer();

}

}

java聊天小程序

不知道你為什么要用這個 流 DataInputStream sin = new DataInputStream(System.in);

你程序沒反應(yīng)是你讀的時候一直阻塞著。你用 BufferedReader試試 肯定有反應(yīng)。


分享文章:java聊天小程序源代碼 java簡單聊天程序代碼
本文來源:http://biofuelwatch.net/article/ddsdooo.html
日韩在线不卡一区二区三区四区五区,欧美视频一区二区三,欧美视频中文字幕一区二区,高端 精品 国产 探花,亚欧无吗一二三四五六区