想写一个一人对一人的聊天程序可是遇到了些问题;
这是客户端代码,其中send是一个发送按钮,想按下后把文本域中的内容发出
且定义了 TextField serverMessage,clientMessage;和TextArea messageDisplay想把从服务器端的内容在messageDisplay上显示出来。可是老是失败。经过测试发现1,2处的打印语句始终不能运行。望高手能给我分析一下原因。谢谢先~~客户端代码
public void runClient()
{
try
{
final Socket s=new Socket(InetAddress.getByName("localhost"),6000);
final OutputStream os=s.getOutputStream();
final InputStream is=s.getInputStream();
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{ String str=serverMessage.getText();
os.write(str.getBytes());
System.out.println(str);//-----------1
}
} catch(IOException ioe){}
}
}); os.write("This is Client".getBytes());
byte[] buf=new byte[1024];
int length=is.read(buf);
messageDisplay.append(new String(buf,0,length));
os.close();
is.close();
s.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
服务器端代码
public void runServer()
{
try
{
final ServerSocket ss=new ServerSocket(6000);//设置服务端口号
final Socket s=ss.accept();
final OutputStream os=s.getOutputStream();
final InputStream is=s.getInputStream();
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String str=clientMessage.getText();
os.write(str.getBytes());
System.out.println(clientMessage.getText());//-----------------2 }
catch(IOException ioe){}
}
});
os.write("This is Server".getBytes());
byte[] buf=new byte[1024];
int length=is.read(buf);
messageDisplay.append(new String(buf,0,length)); os.close();
is.close();
s.close();
ss.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
这是客户端代码,其中send是一个发送按钮,想按下后把文本域中的内容发出
且定义了 TextField serverMessage,clientMessage;和TextArea messageDisplay想把从服务器端的内容在messageDisplay上显示出来。可是老是失败。经过测试发现1,2处的打印语句始终不能运行。望高手能给我分析一下原因。谢谢先~~客户端代码
public void runClient()
{
try
{
final Socket s=new Socket(InetAddress.getByName("localhost"),6000);
final OutputStream os=s.getOutputStream();
final InputStream is=s.getInputStream();
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{ String str=serverMessage.getText();
os.write(str.getBytes());
System.out.println(str);//-----------1
}
} catch(IOException ioe){}
}
}); os.write("This is Client".getBytes());
byte[] buf=new byte[1024];
int length=is.read(buf);
messageDisplay.append(new String(buf,0,length));
os.close();
is.close();
s.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
服务器端代码
public void runServer()
{
try
{
final ServerSocket ss=new ServerSocket(6000);//设置服务端口号
final Socket s=ss.accept();
final OutputStream os=s.getOutputStream();
final InputStream is=s.getInputStream();
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String str=clientMessage.getText();
os.write(str.getBytes());
System.out.println(clientMessage.getText());//-----------------2 }
catch(IOException ioe){}
}
});
os.write("This is Server".getBytes());
byte[] buf=new byte[1024];
int length=is.read(buf);
messageDisplay.append(new String(buf,0,length)); os.close();
is.close();
s.close();
ss.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货