怎么:实现两台计算机之间交互式数据通信的源代码。以窗口实现交互的 java
答案:1 悬赏:80 手机版
解决时间 2021-02-27 05:56
- 提问者网友:欲望失宠
- 2021-02-26 20:57
怎么:实现两台计算机之间交互式数据通信的源代码。以窗口实现交互的 java
最佳答案
- 五星知识达人网友:独钓一江月
- 2021-02-26 21:17
一个简单的聊天程序
参考一下;
//客户端的
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();
}
}
//服务器端的
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();
}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return;
}
}
}
}追问你运行过没
能用?追答你的好评是我的动力!望采纳,谢谢
参考一下;
//客户端的
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();
}
}
//服务器端的
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();
}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return;
}
}
}
}追问你运行过没
能用?追答你的好评是我的动力!望采纳,谢谢
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯