Java 聊天小程序

发布网友

我来回答

3个回答

热心网友

不知道你为什么要用这个

datainputstream
sin
=
new
datainputstream(system.in);
你程序没反应是你读的时候一直阻塞着。你用
bufferedreader试试
肯定有反应。

热心网友

至于作业的话
还是找一个比较专业的
论坛
看看csdn
应该
不错

热心网友

客户端:

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

public class Main {
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private TextArea ta;
private TextField tf;

public static void main(String[] args) {
Main m=new Main();
m.createUI();
m.connect();
m.createThread();

}
public void connect(){
try{s=new Socket("127.0.0.1",8888);//127.0.0.1是你自己的ip号
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());}
catch(IOException e){ e.printStackTrace();}

}
public void createUI(){
Frame f=new Frame("Client");
ta=new TextArea();
tf=new TextField();
Button send=new Button("发送");
Panel p=new Panel();
p.setLayout(new BorderLayout());
p.add(tf,"Center");
p.add(send,"East");
f.add(ta,"Center");
f.add(p,"South");
MyClientListener listener=new MyClientListener(this);
send.addActionListener(listener);
tf.addActionListener(listener);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(400,400);
f.setLocation(600,0);
f.setVisible(true);

}
public void createThread(){
MyClientReader reader=new MyClientReader(this);
reader.start();

}

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

}

public DataInputStream getDataInputStream(){ return dis;}
public DataOutputStream getDataOutputStream(){return dos;}
public TextArea getTextArea(){return ta;}
public TextField getTextField(){return tf;}
}

class MyClientListener implements ActionListener{
private Main client;
public MyClientListener(Main client){
this.client=client;
}
public void actionPerformed(ActionEvent e){
TextField tf=client.getTextField();
String info=tf.getText();
client.getTextArea().append("自己说:"+info+"\n");
try{
client.getDataOutputStream().writeUTF(info);
}
catch(IOException e1){e1.printStackTrace();}
if (info.equals("bye")){
client.close();
System.exit(0);
}
tf.setText("");
tf.requestFocus();

}

}

class MyClientReader extends Thread {
private Main client;
public MyClientReader(Main client){
this.client=client;
}
public void run(){
String info;
DataInputStream dis=client.getDataInputStream();
TextArea ta=client.getTextArea();
try{
while (true){
info=dis.readUTF();
ta.append("对方说:"+info+"\n");
if (info.equals("bye")){
client.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}

服务器端:
package serversocket;

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

public class Main {
private ServerSocket ss;
private Socket s;
private DataInputStream dis;
private DataOutputStream dos;
private TextArea ta;
private TextField tf;

public static void main(String[] args) {
Main m=new Main();
m.createUI();
m.connect();
m.createThread();

}
public void connect(){
try{ss=new ServerSocket(8888); s=ss.accept();
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());}
catch(IOException e){ e.printStackTrace();}

}
public void createUI(){
Frame f=new Frame("Server");
ta=new TextArea();
tf=new TextField();
Button send=new Button("发送");
Panel p=new Panel();
p.setLayout(new BorderLayout());
p.add(tf,"Center");
p.add(send,"East");
f.add(ta,"Center");
f.add(p,"South");
MyServerListener listener=new MyServerListener(this);
send.addActionListener(listener);
tf.addActionListener(listener);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
f.setSize(400,400);
f.setLocation(600,0);
f.setVisible(true);

}
public void createThread(){
MyServerReader reader=new MyServerReader(this);
reader.start();

}

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

}

public DataInputStream getDataInputStream(){ return dis;}
public DataOutputStream getDataOutputStream(){return dos;}
public TextArea getTextArea(){return ta;}
public TextField getTextField(){return tf;}
}

class MyServerListener implements ActionListener{
private Main client;
public MyServerListener(Main client){
this.client=client;
}
public void actionPerformed(ActionEvent e){
TextField tf=client.getTextField();
String info=tf.getText();
client.getTextArea().append("自己说:"+info+"\n");
try{
client.getDataOutputStream().writeUTF(info);
}
catch(IOException e1){e1.printStackTrace();}
if (info.equals("bye")){
client.close();
System.exit(0);
}
tf.setText("");
tf.requestFocus();

}

}

class MyServerReader extends Thread {
private Main client;
public MyServerReader(Main client){
this.client=client;
}
public void run(){
String info;
DataInputStream dis=client.getDataInputStream();
TextArea ta=client.getTextArea();
try{
while (true){
info=dis.readUTF();
ta.append("对方说:"+info+"\n");
if (info.equals("bye")){
client.close();
System.exit(0);
}
}
}
catch(IOException e){
e.printStackTrace();
}
}
}

要先运行服务器端,再运行客户端!

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com