2 u010076940 u010076940 于 2014.10.08 01:28 提问

Java网络聊天室无法接收消息

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

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
public class Server extends JFrame
{
private JTextArea jTextArea;
private JTextField jTextField;
InputStream inputStream;
OutputStream outputStream;
static ServerSocket serverSocket;
Socket socket;
public Server()
{
////窗体///////////////////////////////////////
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame jFrame=new JFrame("我是服务器端");//新建窗体
jFrame.setBounds(200, 200, 420, 400);
jFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});

/////文本域//////////////////////////////////////
jTextArea=new JTextArea();//新建文本域
jTextArea.setEditable(false);//设置不可编辑属性
JScrollPane jScrollPane=new JScrollPane(jTextArea);
jFrame.getContentPane().add(jScrollPane,"Center");//添加到窗体

//////面版和按钮属性事件//////////////////////////////////
JPanel jPanel_1=new JPanel();
JLabel jLabel=new JLabel("请输入消息");
jTextField=new JTextField(20);
jPanel_1.add(jTextField);

JPanel jPanel_2=new JPanel();
JButton jButton_1=new JButton("发送");
jButton_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jTextArea.append("你说:"+jTextField.getText()+"\n");
connectSend();
jTextField.setText("");
}
});
JButton jButton_2=new JButton("取消");
jButton_2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
jTextField.setText("");
}
});

jPanel_1.add(jButton_1);
jPanel_1.add(jButton_2);

JPanel jPanel_12=new JPanel(new BorderLayout());
jPanel_12.add(jScrollPane,"Center");//添加到窗体
jPanel_12.add(new JButton("打个招呼"),"East");
jPanel_12.add(jPanel_1,"South");
jFrame.getContentPane().add(jPanel_12);
jFrame.show();
}

public void connectRecive()
{
try
{
String str;
socket=serverSocket.accept();

inputStream=socket.getInputStream();
/(注释)BufferedInputStream bufferInput=new BufferedInputStream(inputStream);
byte[] bty=new byte[100];
int len=bufferInput.read(bty);
str=new String(bty,0,len);*/(注释)
*

BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));//sss
String s =new String();//sssssssssss

while(true)
{
s=in.readLine();//ssssss
jTextArea.append("对方说:"+s+"\n");
if(s.equals("exit"))
break;
}
in.close();
inputStream.close();

socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void connectSend()
{
try
{
outputStream=socket.getOutputStream();
PrintWriter out=new PrintWriter(outputStream);
out.write(jTextField.getText());
out.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public static void main(String [] args) throws Exception
{
serverSocket=new ServerSocket(6007);
Server server=new Server();
server.connectRecive();
}
}
求指教:在conectRecieve()函数里,我把注释部分换成了加 sssss 的那几行,为什么就不能从客户端接收消息了呢(没有换的时候可以接收)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!