java中Socket通信

java\u7f16\u7a0b\u4e2d\uff0cSocket\u901a\u4fe1\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff1f

java\u7f16\u7a0b\u5bf9\u4e8eSocket\u4e4b\u95f4\u7684\u901a\u4fe1\u8fc7\u7a0b\u5982\u4e0b\uff1a
\u670d\u52a1\u7aef\u5f80Socket\u7684\u8f93\u51fa\u6d41\u91cc\u9762\u5199\u4e1c\u897f\uff0c\u5ba2\u6237\u7aef\u5c31\u53ef\u4ee5\u901a\u8fc7Socket\u7684\u8f93\u5165\u6d41\u8bfb\u53d6\u5bf9\u5e94\u7684\u5185\u5bb9\u3002Socket\u4e0eSocket\u4e4b\u95f4\u662f\u53cc\u5411\u8fde\u901a\u7684\uff0c\u6240\u4ee5\u5ba2\u6237\u7aef\u4e5f\u53ef\u4ee5\u5f80\u5bf9\u5e94\u7684Socket\u8f93\u51fa\u6d41\u91cc\u9762\u5199\u4e1c\u897f\uff0c\u7136\u540e\u670d\u52a1\u7aef\u5bf9\u5e94\u7684Socket\u7684\u8f93\u5165\u6d41\u5c31\u53ef\u4ee5\u8bfb\u51fa\u5bf9\u5e94\u7684\u5185\u5bb9\u3002\u4e0b\u9762\u6765\u770b\u4e00\u4e9b\u670d\u52a1\u7aef\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1\u7684\u4f8b\u5b50\uff1a
public class Server { public static void main(String args[]) throws IOException { //\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6240\u6709\u7684\u5f02\u5e38\u4fe1\u606f\u90fd\u5f80\u5916\u629b int port = 8899; //\u5b9a\u4e49\u4e00\u4e2aServerSocket\u76d1\u542c\u5728\u7aef\u53e38899\u4e0a ServerSocket server = new ServerSocket(port); //server\u5c1d\u8bd5\u63a5\u6536\u5176\u4ed6Socket\u7684\u8fde\u63a5\u8bf7\u6c42\uff0cserver\u7684accept\u65b9\u6cd5\u662f\u963b\u585e\u5f0f\u7684 Socket socket = server.accept(); //\u8ddf\u5ba2\u6237\u7aef\u5efa\u7acb\u597d\u8fde\u63a5\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u83b7\u53d6socket\u7684InputStream\uff0c\u5e76\u4ece\u4e2d\u8bfb\u53d6\u5ba2\u6237\u7aef\u53d1\u8fc7\u6765\u7684\u4fe1\u606f\u4e86\u3002 Reader reader = new InputStreamReader(socket.getInputStream()); char chars[] = new char[64]; int len; StringBuilder sb = new StringBuilder(); while ((len=reader.read(chars)) != -1) { sb.append(new String(chars, 0, len)); } System.out.println("from client: " + sb); reader.close(); socket.close(); server.close(); } }\u5ba2\u6237\u7aef\u4ee3\u7801Java\u4ee3\u7801 public class Client { public static void main(String args[]) throws Exception { //\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6240\u6709\u7684\u5f02\u5e38\u90fd\u76f4\u63a5\u5f80\u5916\u629b String host = "127.0.0.1"; //\u8981\u8fde\u63a5\u7684\u670d\u52a1\u7aefIP\u5730\u5740 int port = 8899; //\u8981\u8fde\u63a5\u7684\u670d\u52a1\u7aef\u5bf9\u5e94\u7684\u76d1\u542c\u7aef\u53e3 //\u4e0e\u670d\u52a1\u7aef\u5efa\u7acb\u8fde\u63a5 Socket client = new Socket(host, port); //\u5efa\u7acb\u8fde\u63a5\u540e\u5c31\u53ef\u4ee5\u5f80\u670d\u52a1\u7aef\u5199\u6570\u636e\u4e86 Writer writer = new OutputStreamWriter(client.getOutputStream()); writer.write("Hello Server."); writer.flush();//\u5199\u5b8c\u540e\u8981\u8bb0\u5f97flush writer.close(); client.close(); } }

Java socket\u901a\u4fe1\u5728Java\u8bed\u8a00\u4e2d\u662f\u4e00\u4e2a\u4f7f\u7528\u5f88\u5e7f\u6cdb\u7684\u5de5\u5177\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u6765\u4ed4\u7ec6\u7684\u5b66\u4e60\u4e0b\u6709\u5173\u7684\u65b9\u6cd5\u3002\u8fd9\u5bf9\u5927\u5bb6\u5728\u4ee5\u540e\u7684\u4f7f\u7528\u4e2d\u6709\u6240\u5e2e\u52a9\uff0c\u53ea\u6709\u4e0d\u65ad\u7684\u5b66\u4e60\u624d\u80fd\u66f4\u597d\u7684\u4f7f\u7528\u3002

\u6240\u8c13Java socket\u901a\u4fe1\u901a\u5e38\u4e5f\u79f0\u4f5c"\u5957\u63a5\u5b57"\uff0c\u7528\u4e8e\u63cf\u8ff0IP\u5730\u5740\u548c\u7aef\u53e3\uff0c\u662f\u4e00\u4e2a\u901a\u4fe1\u94fe\u7684\u53e5\u67c4\u3002\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u901a\u8fc7"\u5957\u63a5\u5b57"\u5411\u7f51\u7edc\u53d1\u51fa\u8bf7\u6c42\u6216\u8005\u5e94\u7b54\u7f51\u7edc\u8bf7\u6c42\u3002

\u4ee5J2SDK-1.3\u4e3a\u4f8b\uff0cSocket\u548cServerSocket\u7c7b\u5e93\u4f4d\u4e8ejava.net\u5305\u4e2d\u3002ServerSocket\u7528\u4e8e\u670d\u52a1\u5668\u7aef\uff0c Socket\u662f\u5efa\u7acb\u7f51\u7edc\u8fde\u63a5\u65f6\u4f7f\u7528\u7684\u3002\u5728\u8fde\u63a5\u6210\u529f\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u4e24\u7aef\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2aSocket\u5b9e\u4f8b\uff0c\u64cd\u4f5c\u8fd9\u4e2a\u5b9e\u4f8b\uff0c\u5b8c\u6210\u6240\u9700\u7684\u4f1a\u8bdd\u3002\u5bf9\u4e8e\u4e00\u4e2a\u7f51\u7edc\u8fde\u63a5\u6765\u8bf4\uff0c \u5957\u63a5\u5b57\u662f\u5e73\u7b49\u7684\uff0c\u5e76\u6ca1\u6709\u5dee\u522b\uff0c\u4e0d\u56e0\u4e3a\u5728\u670d\u52a1\u5668\u7aef\u6216\u5728\u5ba2\u6237\u7aef\u800c\u4ea7\u751f\u4e0d\u540c\u7ea7\u522b\u3002\u4e0d\u7ba1\u662fSocket\u8fd8\u662fServerSocket\u5b83\u4eec\u7684\u5de5\u4f5c\u90fd\u662f\u901a\u8fc7 SocketImpl\u7c7b\u53ca\u5176\u5b50\u7c7b\u5b8c\u6210\u7684\u3002

\u91cd\u8981\u7684Socket API\uff1a

java.net.Socket\u7ee7\u627f\u4e8ejava.lang.Object\uff0c\u6709\u516b\u4e2a\u6784\u9020\u5668\uff0c\u5176\u65b9\u6cd5\u5e76\u4e0d\u591a\uff0c\u4e0b\u9762\u4ecb\u7ecd\u4f7f\u7528\u6700\u9891\u7e41\u7684\u4e09\u4e2a\u65b9\u6cd5\uff0c\u5176\u5b83\u65b9\u6cd5\u5927\u5bb6\u53ef\u4ee5\u89c1JDK-1.3\u6587\u6863\u3002Accept\u65b9\u6cd5\u7528\u4e8e\u4ea7\u751f"\u963b\u585e"\uff0c\u76f4\u5230\u63a5\u53d7\u5230\u4e00\u4e2a\u8fde\u63a5\uff0c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u5ba2\u6237\u7aef\u7684Socket\u5bf9\u8c61\u5b9e\u4f8b\u3002"\u963b\u585e"\u662f\u4e00\u4e2a\u672f\u8bed\uff0c\u5b83\u4f7f\u7a0b\u5e8f\u8fd0\u884c\u6682\u65f6"\u505c\u7559"\u5728\u8fd9\u4e2a\u5730\u65b9\uff0c\u76f4\u5230\u4e00\u4e2a\u4f1a\u8bdd\u4ea7\u751f\uff0c\u7136\u540e\u7a0b\u5e8f\u7ee7\u7eed\uff1b\u901a\u5e38"\u963b\u585e"\u662f\u7531\u5faa\u73af\u4ea7\u751f\u7684\u3002

getInputStream\u65b9\u6cd5\u83b7\u5f97\u7f51\u7edc\u8fde\u63a5\u8f93\u5165\uff0c\u540c\u65f6\u8fd4\u56de\u4e00\u4e2aIutputStream\u5bf9\u8c61\u5b9e\u4f8b\u3002

getOutputStream\u65b9\u6cd5\u8fde\u63a5\u7684\u53e6\u4e00\u7aef\u5c06\u5f97\u5230\u8f93\u5165\uff0c\u540c\u65f6\u8fd4\u56de\u4e00\u4e2aOutputStream\u5bf9\u8c61\u5b9e\u4f8b\u3002

\u6ce8\u610f\uff1a\u5176\u4e2dgetInputStream\u548cgetOutputStream\u65b9\u6cd5\u5747\u4f1a\u4ea7\u751f\u4e00\u4e2aIOException\uff0c\u5b83\u5fc5\u987b\u88ab\u6355\u83b7\uff0c\u56e0\u4e3a\u5b83\u4eec\u8fd4\u56de\u7684\u6d41\u5bf9\u8c61\uff0c\u901a\u5e38\u90fd\u4f1a\u88ab\u53e6\u4e00\u4e2a\u6d41\u5bf9\u8c61\u4f7f\u7528\u3002

\u5982\u4f55\u5f00\u53d1\u4e00\u4e2aServer-Client\u6a21\u578b\u7684\u7a0b\u5e8f

Java socket\u901a\u4fe1\u5f00\u53d1\u539f\u7406\uff1a

\u670d\u52a1\u5668\uff0c\u4f7f\u7528ServerSocket\u76d1\u542c\u6307\u5b9a\u7684\u7aef\u53e3\uff0c\u7aef\u53e3\u53ef\u4ee5\u968f\u610f\u6307\u5b9a\uff08\u7531\u4e8e1024\u4ee5\u4e0b\u7684\u7aef\u53e3\u901a\u5e38\u5c5e\u4e8e\u4fdd\u7559\u7aef\u53e3\uff0c\u5728\u4e00\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u4e0d\u53ef\u4ee5\u968f\u610f\u4f7f\u7528\uff0c\u6240\u4ee5\u5efa\u8bae\u4f7f\u7528\u5927\u4e8e1024\u7684\u7aef\u53e3\uff09\uff0c\u7b49\u5f85\u5ba2\u6237\u8fde\u63a5\u8bf7\u6c42\uff0c\u5ba2\u6237\u8fde\u63a5\u540e\uff0c\u4f1a\u8bdd\u4ea7\u751f\uff1b\u5728\u5b8c\u6210\u4f1a\u8bdd\u540e\uff0c\u5173\u95ed\u8fde\u63a5\u3002

\u5ba2\u6237\u7aef\uff0c\u4f7f\u7528Java socket\u901a\u4fe1\u5bf9\u7f51\u7edc\u4e0a\u67d0\u4e00\u4e2a\u670d\u52a1\u5668\u7684\u67d0\u4e00\u4e2a\u7aef\u53e3\u53d1\u51fa\u8fde\u63a5\u8bf7\u6c42\uff0c\u4e00\u65e6\u8fde\u63a5\u6210\u529f\uff0c\u6253\u5f00\u4f1a\u8bdd\uff1b\u4f1a\u8bdd\u5b8c\u6210\u540e\uff0c\u5173\u95edSocket\u3002\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u6307\u5b9a\u6253\u5f00\u7684\u7aef\u53e3\uff0c\u901a\u5e38\u4e34\u65f6\u7684\u3001\u52a8\u6001\u7684\u5206\u914d\u4e00\u4e2a1024\u4ee5\u4e0a\u7684\u7aef\u53e3\u3002

你加了高分我才贴
----------------具体是: Client-A发送消息向Server:消息包括内容+流向(Client-B的地址)+消息来源地址 Server接收后再把消息+来源地址发给Client-B

两个文件都给你
肯定没有错的

//ChatServer.java

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {
boolean started = false;
ServerSocket ss = null;

List<Client> clients = new ArrayList<Client>();

public static void main(String[] args) {
new ChatServer().start();
}

public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}

try {

while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
//dis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;

public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}

public void send(String str) {
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("对方退出了!我从List里面去掉了!");
//e.printStackTrace();
}
}

public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
System.out.println(str);
for(int i=0; i<clients.size(); i++) {
Client c = clients.get(i);
c.send(str);
//System.out.println(" a string send !");
}
/*
for(Iterator<Client> it = clients.iterator(); it.hasNext(); ) {
Client c = it.next();
c.send(str);
}
*/
/*
Iterator<Client> it = clients.iterator();
while(it.hasNext()) {
Client c = it.next();
c.send(str);
}
*/
}
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) {
s.close();
//s = null;
}

} catch (IOException e1) {
e1.printStackTrace();
}

}
}

}
}

//ChatClient.java

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis = null;
private boolean bConnected = false;

TextField tfTxt = new TextField();

TextArea taContent = new TextArea();

Thread tRecv = new Thread(new RecvThread());

public static void main(String[] args) {
new ChatClient().launchFrame();
}

public void launchFrame() {
setLocation(400, 300);
this.setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {

@Override
public void windowClosing(WindowEvent arg0) {
disconnect();
System.exit(0);
}

});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();

tRecv.start();
}

public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

public void disconnect() {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}

/*
try {
bConnected = false;
tRecv.join();
} catch(InterruptedException e) {
e.printStackTrace();
} finally {
try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
*/
}

private class TFListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");

try {
//System.out.println(s);
dos.writeUTF(str);
dos.flush();
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}

}

}

private class RecvThread implements Runnable {

public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText() + str + '\n');
}
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("推出了,bye - bye!");
} catch (IOException e) {
e.printStackTrace();
}

}

}
}

看看这个怎么样:
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import ccit.Message;

public class Server extends Thread
{
public static final int PORT = 1112;
Socket socket;
ServerSocket serverSocket;

static Vector usrList=new Vector(1,1);

public Server()
{
try
{
serverSocket=new ServerSocket(PORT);
}
catch(IOException ioe)
{
System.out.println("Can't set up server socket."+ioe);
}
System.out.println("server start ...");
this.start();
}
public void run()
{
try
{
while (true)
{
System.out.println("正在等待连接.....");
socket = serverSocket.accept();
//PrintWriter pw=new PrintWriter(socket.getOutputStream());
// pw.println("aaa");
usrThread ut=new usrThread(socket);
usrList.addElement(ut);
}
}
catch(IOException ioe1)
{
System.out.println("Can't set up user thread"+ioe1);
}
}

public static void main(String args[])
{
Server sobj=new Server();

}
}

class usrThread extends Thread
{
Socket socket;
ObjectInputStream osFromClient;
ObjectOutputStream osToClient;

thPut tp;
thGet tg;

static int msgCount=0;
static Vector msgBox = new Vector(1,1);

int localCount=0;

public synchronized void writeMsg(Message msg)
{
msgBox.addElement(msg);
msgCount++;

}
public synchronized Message readMsg()
{
Message msg=(Message)(msgBox.elementAt(localCount));
return msg;
}

public usrThread(Socket s)
{
socket=s;
try
{
osFromClient=new ObjectInputStream(socket.getInputStream());
osToClient=new ObjectOutputStream(socket.getOutputStream());
System.out.println("osToClient and osFromClient finished!");

this.start();
tp=new thPut();
tp.start();
tg=new thGet();
tg.start();
}
catch(Exception e)
{
System.out.println("usrThread init error! "+e);

}

}
public void run()
{

}
class thPut extends Thread
{
Message msg;

public void run()
{
try
{
while(true)
{
msg = (Message)osFromClient.readObject();
writeMsg(msg);
System.out.println(msg.fromname+" says: "+msg.message);
}
}
catch(Exception e)
{
System.out.println("Receive error"+e);
}
}

}

class thGet extends Thread
{
//Declare the currnet message get from readMsg method
Message msg=new Message();
public void run()
{
try
{
while(true)
{

while (localCount>=msgCount)
{
this.sleep(1);
}
msg=readMsg();
System.out.println("Write to "+msg.toname);
osToClient.writeObject(msg);

System.out.println(String.valueOf(localCount));
localCount++;
}

}
catch(Exception e)
{
System.out.println("cant write msg to client"+e);

}
}

}

}
客户端代码:
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import ccit.Message;

public class ClientChat extends JFrame
{

static long sum;
JLabel lpublic=new JLabel("公共聊天区");
JLabel lprivate=new JLabel("私人聊天区");
JLabel lgetlist=new JLabel("在线用户");

JTextArea taPublicMsg=new JTextArea(10,15);
JTextArea taPrivateMsg=new JTextArea(6,10);

JTextField tfSendMsg=new JTextField(40);

JButton bsend=new JButton("发送");
JButton bexit=new JButton("退出");

JPanel pLabelPub=new JPanel();
JPanel pLabelPri=new JPanel();

Vector userVector=new Vector(1,1);

JList userList=new JList();

Socket socket=null;

String name;

ObjectOutputStream oos=null;
ObjectInputStream ois=null;

public ClientChat(String name)
{

JScrollPane scrpPub=new JScrollPane(taPublicMsg);
pLabelPub.setLayout(new BorderLayout());
pLabelPub.add(lpublic,BorderLayout.NORTH);
pLabelPub.add(scrpPub,BorderLayout.CENTER);

JScrollPane scrpPri=new JScrollPane(taPrivateMsg);
pLabelPri.setLayout(new BorderLayout());
pLabelPri.add(lprivate,BorderLayout.NORTH);
pLabelPri.add(scrpPri,BorderLayout.CENTER);

JPanel pGetMsg=new JPanel();
pGetMsg.setLayout(new GridLayout(3,1,5,5));
pGetMsg.add(pLabelPub);
//pGetMsg.add(taPublicMsg);
pGetMsg.add(pLabelPri);
//pGetMsg.add(taPrivateMsg);

JPanel pSendMsg=new JPanel();
pSendMsg.setLayout(new FlowLayout());
pSendMsg.add(tfSendMsg);
pSendMsg.add(bsend);
pSendMsg.add(bexit);

pGetMsg.add(pSendMsg);

userVector.addElement("To All");
userList.setListData(userVector);
JScrollPane scrpList=new JScrollPane(userList);

JPanel pGetList=new JPanel();
pGetList.setLayout(new BorderLayout());
pGetList.add(lgetlist,BorderLayout.NORTH);
pGetList.add(scrpList,BorderLayout.CENTER);

this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(pGetMsg,BorderLayout.CENTER);
this.getContentPane().add(pGetList,BorderLayout.EAST);
this.setTitle("小桥流水(花语)聊天室(在线用户"+(++sum)+"位)");
this.setSize(500,300);
this.setVisible(true);

this.name=name;
//this.socket=socket;

try
{
System.out.println("正在连接.....");
InetAddress addr = InetAddress.getByName(null);
System.out.println("addr = " + addr);

//socket = new Socket(addr,Server.PORT);
//socket = new Socket("127.0.0.1",1112);

ois=new ObjectInputStream(socket.getInputStream());
oos=new ObjectOutputStream(socket.getOutputStream());

//PrintWriter pw=new PrintWriter(oos);

Message msgLogin=new Message("To All",name,"Login","Login");

//pw.print(msgLogin);
oos.writeObject(msgLogin);
new manageThread().start();

}
catch(Exception e)
{
System.out.println("JFtame error:"+e);
}
}

public class sendListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Message getMsg=new Message();
getMsg.toname=userList.getSelectedValue().toString();
getMsg.fromname=name;
getMsg.message=tfSendMsg.getText();
getMsg.command="chat";
try
{
oos.writeObject(getMsg);
}
catch(Exception e)
{
System.out.println("Send:"+getMsg.message);
}
}
}

public class exitListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Message getMsg=new Message("To All",name,"","Logout");
try
{
oos.writeObject(getMsg);
}
catch(Exception e)
{
System.out.println(e);
}
ClientChat.this.setVisible(false);
}
}

public class manageThread extends Thread
{
Message GetMessage;
public void run()
{
while(true)
{
try
{
GetMessage=(Message)ois.readObject();
if(GetMessage.command.equals("Login"))
{
userVector.addElement(GetMessage.fromname);
userList.setListData(userVector);
taPublicMsg.append(GetMessage.fromname+"has arrived!\n");
}

if(GetMessage.command.equals("Logout"))
{
int i=0;
for(i=0;i<userVector.size();i++)
{
if(((String)userVector.elementAt(i)).equals(GetMessage.fromname))
{
System.out.println("i="+i);
break;
}
}
if(i<userVector.size())
{
userVector.removeElementAt(i);
userList.updateUI();
}
taPublicMsg.append(GetMessage.fromname+"has left!\n");
}

if(GetMessage.command.equals("chat"))
{
if(GetMessage.toname.equals(name))
{
taPrivateMsg.append(GetMessage.fromname+"say to me:"+GetMessage.message+"\n");
}
else
{
taPublicMsg.append(GetMessage.fromname+"say to all:"+GetMessage.message+"\n");
}
}
}
catch(Exception e)
{
System.out.println("GetMessage error:"+e);
break;
}

}
}

}
/* public static void main(String args[])
{
Socket socket=null;
new ClientChat(socket,"all");
}*/

}

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import ccit.Message;
public class ClientLogin extends JApplet
{
JLabel lname=new JLabel("用户名:");
JLabel lpasswd=new JLabel("密 码:");
JTextField tname=new JTextField(10);
JTextField tpasswd=new JTextField(10);
JButton blogin=new JButton("登录");
//Socket socket=null;
public void init()
{
JPanel pup=new JPanel();
JPanel pdown=new JPanel();

pup.setLayout(new GridLayout(2,2,5,5));
pup.add(lname);
pup.add(tname);
pup.add(lpasswd);
pup.add(tpasswd);

pdown.setLayout(new FlowLayout());
pdown.add(blogin);

blogin.addActionListener(new loginListener());

this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(pup,BorderLayout.CENTER);
this.getContentPane().add(pdown,BorderLayout.SOUTH);
}

public class loginListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==blogin)
{
try
{
//Socket socket=new Socket(InetAddress.getLocalHost(),7000);
Socket socket=new Socket("127.0.0.1",1112);
ClientChat customer=new ClientChat(tname.getText());
}
catch(Exception e)
{
System.out.println("Client error:"+e);
return;
}
}
}
}
}

package ccit;
import java.io.*;
public class Message implements Serializable
{
public String toname;
public String fromname;
public String message;
public String command;

public Message()
{

}
public Message(String to,String from,String msg,String command)
{
this.toname=to;
this.fromname=from;
this.message=msg;
this.command=command;
}

}

客户端要在Applet运行的。

  • Java Socket璇存槑
    绛旓細Java Socket锛屼篃琚О浣"濂楁帴瀛"锛屾槸涓绉嶇敤浜庡湪缃戠粶涓爣璇咺P鍦板潃鍜岀鍙鐨勯氫俊鏈哄埗锛屽畠鍏呭綋浜嗛氫俊閾捐矾鐨勫鐞嗕腑蹇冦傚湪搴旂敤绋嬪簭涓紝Socket閫氬父琚敤鏉ュ彂璧锋垨鍝嶅簲缃戠粶璇锋眰銆傚湪Java寮鍙戜腑锛孲ocket鍜孲erverSocket鐨鍔熻兘涓昏闆嗕腑鍦java.net鍖呯殑J2SDK-1.3鐗堟湰涓係erverSocket涓昏璐熻矗鏈嶅姟鍣ㄧ锛岃孲ocket鍒欐槸瀹㈡埛绔繘...
  • Java Socket瀹㈡埛绔鐨渚嬪瓙
    绛旓細鍦Java缂栫▼涓紝瑕佸垱寤轰竴涓鎴风涓庢湇鍔″櫒杩涜閫氫俊锛岄鍏堥渶瑕佸畾涔夎杩炴帴鐨勬湇鍔″櫒鐨勪睛鍚鍙c備緥濡傦紝浣犲彲浠ヨ缃竴涓父閲忥細int PORT = 8888; // 瀹氫箟鏈嶅姟鍣ㄤ睛鍚殑绔彛 鎺ョ潃锛屼綘闇瑕佷娇鐢⊿ocket绫绘潵寤虹珛瀹㈡埛绔笌鏈嶅姟鍣ㄤ箣闂寸殑杩炴帴銆傝繖涓繃绋嬫秹鍙婂垱寤轰竴涓柊鐨凷ocket瀵硅薄锛屾寚瀹氭湇鍔″櫒鐨処P鍦板潃鍜岀鍙e彿锛歋ocket s...
  • java Socket閫氫俊鍘熺悊
    绛旓細棣栧厛socket 閫氫俊鏄熀浜嶵CP/IP 缃戠粶灞備笂鐨涓绉嶄紶閫佹柟寮忥紝鎴戜滑閫氬父鎶奣CP鍜孶DP绉颁负浼犺緭灞傘傚叾涓璘DP鏄竴绉嶉潰鍚戞棤杩炴帴鐨勪紶杈撳眰鍗忚銆俇DP涓嶅叧蹇冨绔槸鍚︾湡姝f敹鍒颁簡浼犻佽繃鍘荤殑鏁版嵁銆傚鏋滈渶瑕佹鏌ュ绔槸鍚︽敹鍒板垎缁勬暟鎹寘锛屾垨鑰呭绔槸鍚﹁繛鎺ュ埌缃戠粶锛屽垯闇瑕佸湪搴旂敤绋嬪簭涓疄鐜般俇DP甯哥敤鍦ㄥ垎缁勬暟鎹緝灏戞垨澶氭挱銆佸箍鎾氫俊...
  • java缂栫▼涓,Socket閫氫俊鏄庝箞瀹炵幇鐨?
    绛旓細java缂栫▼瀵逛簬Socket涔嬮棿鐨閫氫俊杩囩▼濡備笅锛氭湇鍔$寰Socket鐨勮緭鍑烘祦閲岄潰鍐欎笢瑗匡紝瀹㈡埛绔氨鍙互閫氳繃Socket鐨勮緭鍏ユ祦璇诲彇瀵瑰簲鐨勫唴瀹广係ocket涓嶴ocket涔嬮棿鏄弻鍚戣繛閫氱殑锛屾墍浠ュ鎴风涔熷彲浠ュ線瀵瑰簲鐨凷ocket杈撳嚭娴侀噷闈㈠啓涓滆タ锛岀劧鍚庢湇鍔$瀵瑰簲鐨凷ocket鐨勮緭鍏ユ祦灏卞彲浠ヨ鍑哄搴旂殑鍐呭銆備笅闈㈡潵鐪嬩竴浜涙湇鍔$涓庡鎴风閫氫俊鐨勪緥瀛愶細publi...
  • Java 鍜屽叾瀹冭瑷涔嬮棿鎬庝箞杩涜Socket閫氫俊?鐢JAVA缂栧啓server,C璇█缂栧啓Cl...
    绛旓細Java 鍜屽叾瀹冭瑷涔嬮棿杩涜Socket閫氫俊浣跨敤Socket鍜孲erverSocket绫汇傜敤JAVA缂栧啓server锛孋璇█缂栧啓Client锛屽綋鐒跺彲浠ュ疄鐜伴氫俊锛堢ず渚嬪湪鏈鍚庯級銆1銆丼ocket鍜孲erverSocket绫诲簱浣嶄簬java.net鍖呬腑銆係erverSocket鐢ㄤ簬鏈嶅姟鍣ㄧ锛孲ocket 鏄缓绔嬬綉缁滆繛鎺ユ椂浣跨敤鐨勩傚湪杩炴帴鎴愬姛鏃讹紝搴旂敤绋嬪簭涓ょ閮戒細浜х敓涓涓猄ocket瀹炰緥锛屾搷浣滆繖涓疄渚嬶紝...
  • java涓殑socket鏄粈涔堟剰鎬?
    绛旓細鎵璋socket閫氬父涔熺О浣"濂楁帴瀛"锛岀敤浜庢弿杩癐P鍦板潃鍜岀鍙o紝鏄竴涓閫氫俊閾剧殑鍙ユ焺銆傚簲鐢ㄧ▼搴忛氬父閫氳繃"濂楁帴瀛"鍚戠綉缁滃彂鍑鸿姹傛垨鑰呭簲绛旂綉缁滆姹傘俓x0d\x0a浠2SDK-1.3涓轰緥锛Socket鍜孲erverSocket绫诲簱浣嶄簬java.net鍖呬腑銆係erverSocket鐢ㄤ簬鏈嶅姟鍣ㄧ锛孲ocket鏄缓绔嬬綉缁滆繛鎺ユ椂浣跨敤鐨勩傚湪杩炴帴鎴愬姛鏃讹紝搴旂敤绋嬪簭涓ょ閮戒細...
  • java 涓殑socket 鏄笉鏄粠鍝釜绔彛鍙戝嚭瀹㈡埛绔姹傚氨浠庤繖涓鍙f帴鏀舵暟鎹...
    绛旓細Socket socket = new Socket("168.160.12.42",9998);鎴栵細Socket socket = new Socket(InetAddress.getLocalHost(),5678); // 鍚戜富鏈哄悕涓篒netAddress.getLocalHost()鐨鏈嶅姟鍣ㄧ敵璇疯繛鎺 瀹㈡埛鏈哄繀椤荤煡閬撴湁鍏虫湇鍔″櫒鐨処P鍦板潃锛屽浜庣潃涓鐐Java涔熸彁渚涗簡涓涓浉鍏崇殑绫籌netAddress 璇ュ璞$殑瀹炰緥蹇呴』閫氳繃瀹冪殑...
  • Java Socket甯歌寮傚父澶勭悊
    绛旓細鍦java缃戠粶缂栫▼Socket閫氫俊涓 閫氬父浼氶亣鍒颁互涓嬪紓甯告儏鍐 绗 涓紓甯告槸 BindException:Address already in use: JVM_Bind 璇ュ紓甯稿彂鐢熷湪鏈嶅姟鍣ㄧ杩涜new ServerSocket(port)锛坧ort鏄竴涓 鐨勬暣鍨嬪硷級鎿嶄綔鏃 寮傚父鐨勫師鍥犳槸浠ヤ负涓巔ort涓鏍风殑涓涓鍙e凡缁忚鍚姩 骞惰繘琛岀洃鍚 姝ゆ椂鐢╪etstat an鍛戒护 鍙互鐪嬪埌涓涓狶ist...
  • Java TCP/IP Socket缂栫▼鍐呭绠浠
    绛旓細閴翠簬Java鐨骞挎硾鍙楁杩庡害浠ュ強杩戝勾鏉ュ湪鐗堟湰鏇存柊(浠1.5鍒1.7)鍜屽叧閿娊璞★紙濡俷etworkinterface銆乮nterfaceaddress銆乮net4/6address鎶借薄銆socketaddress/inetsocketaddress鍜宔xecutor绛夛級涓婄殑鍒涙柊锛屾湰涔︾殑鏂扮増绱ц窡鏃朵唬娼祦锛岃灏借鐩栦簡Java鐨勬渶鏂版妧鏈唴瀹广傚湪鐜颁唬浜掕仈缃戝簲鐢ㄤ腑锛屽嚑涔庢棤澶勪笉浣跨敤Java Socket杩涜閫氫俊銆傛湰...
  • JavaSocket閫氳濡備綍杩涜瀹㈡埛绔殑淇℃伅閫氫俊
    绛旓細Java Socket閫氳鍦ㄥ疄闄呯殑浣跨敤涓湁寰堝鍏抽敭浠g爜闇瑕佸涔狅紝鐪嬬湅鍦ㄥ鎴风涔嬮棿濡備綍杩涜閭欢鐨勪紶閫掋備笅闈㈢殑浠g爜灏辨槸瀵笿ava Socket閫氳鐩稿叧鍐呭鐨勪粙缁 import java.net.*;import java.io.*;public class Jserver2{ private BufferedReader reader;銆//璐熻矗杈撳叆 private ServerSocket server; //鏈嶅姟鍣ㄥ鎺ュ瓧 priva...
  • 扩展阅读:java入门网站 ... java websocket ... javascript入门 ... java socket服务端 ... java socket tcp ... java ... python websocket ... java通过socket进行通信 ... netlink socket ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网