java聊天小程序

两个文件开两个dos窗口在同一主机上模拟聊天程序先由Client说一句话再Server回一句如此循环编译通过,但执行没结果,求java大神帮助importjava.io.... 两个文件 开两个dos窗口 在同一主机上模拟聊天程序
先由Client说一句话 再Server回一句 如此循环
编译通过,但执行没结果,求java大神帮助
import java.io.*;
import java.net.*;
public class TalkServer {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8888);
Socket socket = server.accept();
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
DataInputStream in = new DataInputStream(socket.getInputStream());
DataInputStream sin = new DataInputStream(System.in);

String str = null;
System.out.println("Client : " + in.readUTF());
while(!(str = sin.readUTF()).equals("bye")) {
out.writeUTF(str);
out.flush();
System.out.println("Server : " + str);
System.out.println("Client : " + in.readUTF());
}
sin.close();
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

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

public class TalkClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1",8888);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
DataInputStream in = new DataInputStream(socket.getInputStream());
DataInputStream sin = new DataInputStream(System.in);
String str = null;
while(!(str = sin.readUTF()).equals("bye")) {
out.writeUTF(str);
out.flush();
System.out.println("Client : " + str);
System.out.println("Server : " + in.readUTF());
}
sin.close();
in.close();
out.close();
socket.close();
} catch (UnknownHostException e) {
System.out.println("连接出错");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
不要给我一个新的程序谢谢 我是在学java 不是交任务 我写的是最简单的点对点程序 有很多不足之处 但我只想知道自己想要完成的功能为什么不能实现
展开
 我来答
_诗礼_
2014-07-02 · 超过69用户采纳过TA的回答
知道小有建树答主
回答量:184
采纳率:50%
帮助的人:107万
展开全部
不知道你为什么要用这个 流 DataInputStream sin = new DataInputStream(System.in);
你程序没反应是你读的时候一直阻塞着。你用 BufferedReader试试 肯定有反应。
落帅0045
2014-07-02 · TA获得超过113个赞
知道答主
回答量:113
采纳率:0%
帮助的人:55.7万
展开全部
客户端:

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();
}
}
}

要先运行服务器端,再运行客户端!
希望能解决您的问题。
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友b1b320b
2014-07-02
知道答主
回答量:24
采纳率:0%
帮助的人:4.8万
展开全部
试一下跑这个代码
客户端:

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();
}
}
}

要先运行服务器端,再运行客户端!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式