java编写的Socket通信,客户端用了GUI页面,服务端还是控制台,结果客户端发送消息后死机了

飞蛾扑火112 2016-03-04 11:47:41
给服务器发送消息后,客户端直接卡住了,但是还能接收到服务器发送过来的消息,并且显示在客户端文本区,但是客户端整个都不能点了
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;


class NetChat{

private Frame f;
private TextArea ta;
private TextField tf;
private Button bConnect,bSend,bClose;

public Socket s = new Socket();
public BufferedReader bufr;
public PrintWriter pwOut;

private int counter = 1;
private final String client = "客户端:";
private final String server = "服务端:";


NetChat(){
init();
}

public void init(){
f = new Frame("我的聊天室");
f.setBounds(400,100,530,750);
f.setLayout(new FlowLayout());
ta = new TextArea(40,70);
tf = new TextField(70);
bConnect = new Button("连接");
bSend = new Button("发送");
bClose = new Button("关闭");



f.add(ta);
f.add(tf);
f.add(bConnect);
f.add(bSend);
f.add(bClose);

myevent();
f.setResizable(false);
f.setVisible(true);
}
public void myevent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//关闭链接
bClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
s.close();
System.exit(0);
}catch(Exception eClose){
System.out.println("关闭链接失败!");
}
}
});
//发送消息
bSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(tf.getText().equals("")){
System.out.println("请输入消息!!!");
}else{
if(s.isConnected()){
//获取输出流
try{
pwOut = new PrintWriter(s.getOutputStream(),true);
String str = tf.getText();
pwOut.println(str);

//pwOut.flush();
ta.append(client+str+"\n");
//pwOut.close();
recMsg();

}catch(Exception eSend){
System.out.println("发送消息异常"+counter++);
}
}else
System.out.println("未连接服务器!");
}

}
});
//建立链接
bConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
s = new Socket("192.168.1.105",10086);
}catch(Exception eLink){
System.out.println("链接出现异常!");
}
}
});
}
public void recMsg(){
try{
//获取输入流
bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
//String str = bufr.readLine();//读取服务端返回的数据
String line = null;
while((line=bufr.readLine())!=null){
ta.append(server+line+"\n");
}
}catch(Exception eRecMsg){
System.out.println("获取消息失败!");
}
}

public static void main(String []args){
new NetChat();
}
}

class ChatServer{

public static void main(String []args)throws Exception{
//建立服务端ServerSocket
ServerSocket ss = new ServerSocket(10086);
//将服务端与客户端链接,并获取客户端对象
Socket s = ss.accept();

String ip = s.getInetAddress().getHostAddress();
int port = s.getPort();

System.out.println(ip+":"+port+"......connected");

//定义服务端输入流
BufferedReader bufrIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

//定义键盘录入
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
//定义服务端输出流
PrintWriter pwOut = new PrintWriter(s.getOutputStream(),true);


String line = null;
while((line=bufrIn.readLine())!=null){//读取客户端发来的数据

System.out.println("客户端:"+line);
pwOut.println("客户端你好");
//System.out.println("服务端写入:"+line);
while((line=bufr.readLine())!=null){
pwOut.println(line);
System.out.println("服务端写入:"+line);
}
}
/*while((line=bufr.readLine())!=null){

}
*/
pwOut.close();
s.close();
ss.close();
}

}





...全文
135 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞蛾扑火112 2016-03-05
  • 打赏
  • 举报
回复

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧