return list;
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
new Thread(new ClientThread(socket)).start();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(socket.getOutputStream());
while (true) {
String msg = reader.readLine();
writer.println(msg);
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ClientThread implements Runnable {
private Socket socket;
private BufferedReader reader;
public ClientThread(Socket socket) {
this.socket = socket;
try {
reader
如何使用java实现两台计算机的通信 源代码一个简单java即时消息源代码的聊天程序
参考一下java即时消息源代码;
//客户端java即时消息源代码的
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ChatClient {
public static void main(String[] args) {
ChatClient cc=new ChatClient();
cc.receive();
}
JTextField jtf;
JTextArea jta;
Socket s;
PrintWriter out;
BufferedReader in;
public ChatClient(){
JFrame frame=new JFrame("ChatClient");
frame.setSize(400,300);
jta=new JTextArea();
jta.setEditable(false);
jtf=new JTextField();
jtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
send();
}
});
frame.getContentPane().add(new JScrollPane(jta));
frame.getContentPane().add(jtf,"South");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try {
s=new Socket("127.0.0.1",9000);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void receive(){
while(true){
try {
String text=in.readLine();
this.jta.append(text+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
}
public void send(){
String text=this.jtf.getText();
this.jtf.setText("");
out.println(text);
out.flush();
}
}
//服务器端java即时消息源代码的
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(9000);
List list=new ArrayList();
while(true){
Socket s=ss.accept();
list.add(s);
Thread t=new ServerThread(s,list);
t.start();
}
}
}
class ServerThread extends Thread{
Socket s;
List list;
BufferedReader in;
PrintWriter out;
public ServerThread(Socket s, List list) {
this.s = s;
this.list = list;
try {
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
try {
String str=in.readLine();
if (str==null) return;
Iterator it=list.iterator();
while(it.hasNext()){
Socket socket=(Socket)(it.next());
PrintWriter o=new PrintWriter(socket.getOutputStream());
o.println(str);
o.flush();
推荐阅读
- 直播话术瑜伽裤,搜索瑜伽直播
- 小米电视3安卓软件怎么用,小米电视三方软件在哪
- 如何做营销短信发送,如何发营销短信内容
- 新媒体直播运营的工作如何,新媒体主播运营职位咋样
- go语言结合php go和php
- 关于营销号如何扒皮的信息
- 怎么转发砍价小程序,怎么把砍价小程序转成链接
- 连接蓝牙命Linux命令 linux命令行蓝牙
- 大壮阿秀唐三什么电视的简单介绍