聊天程序小问题

bbhere 2005-07-14 09:10:44
写的聊天程序,server端运行后有下面错误
Exception in thread "main" java.net.BindException:Address already in use:JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:331)
at java.net.ServerSocket.bind(ServerSocket.java:318)
at java.net.ServerSocket.<init>(ServerSocket.java.185)
at java.net.ServerSocket.<init>(ServerSocket.java.97)
at Server1.main(Server1.java:7)
小弟初学java,看不懂,请高手指点,告诉小弟该如何修改,不胜感激,代码如下:
import java.net.*;
import java.io.*;
public class Server1
{
public static void main(String[] args) throws IOException
{
ServerSocket ss=new ServerSocket(10000);
Socket s=null;
String toClient;
try{
System.out.println("等待客户端连接");
while(true)
{
s=ss.accept();
String ip=""+s.getInetAddress();
System.out.println("IP:"+ip+"登陆");
try{
new ReceiveClientThread(s);
}catch(Exception e){s.close();} }
}catch(IOException e){System.err.println("Failed to connect");}

}
}
class ReceiveClientThread extends Thread{
private Socket s;
private DataInputStream datain;
private DataOutputStream dataout;
private BufferedReader in;
String toClient;
String fromClient;
public ReceiveClientThread(Socket socket) throws IOException {
s=socket;
datain=new DataInputStream(s.getInputStream());
dataout=new DataOutputStream(s.getOutputStream());
in=new BufferedReader(new InputStreamReader(System.in));
start();}
public void run()
{
try{
while(true){
fromClient=datain.readUTF(); System.out.println("client说:"+fromClient);
if(fromClient.equals("quit"))
{
System.out.println("Client exit");
break;
}
toClient=in.readLine();
dataout.writeUTF(toClient);
}
}catch(Exception e){}
}
}
...全文
312 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
bbhere 2005-07-18
  • 打赏
  • 举报
回复
谢谢楼上大哥,不过你得客户端用的是udp,我还不懂
呵呵
有没有和我这个差不多的?
bbhere 2005-07-15
  • 打赏
  • 举报
回复
是这个问题太简单了么?
bbhere 2005-07-15
  • 打赏
  • 举报
回复
急啊,我还等
各位帮忙啊
hifan 2005-07-15
  • 打赏
  • 举报
回复
http://blog.csdn.net/hifan/archive/2005/06/04/387428.aspx
我写的最基本的聊天程序,400行代码。
hifan 2005-07-15
  • 打赏
  • 举报
回复
.flush(); //flush the buffer, send message to the destination
.close();

then:
= null;
bbhere 2005-07-15
  • 打赏
  • 举报
回复
出于对知识的不懈追求,我还等
liudazhong 2005-07-15
  • 打赏
  • 举报
回复
看不出来
bbhere 2005-07-14
  • 打赏
  • 举报
回复
我等
bbhere 2005-07-14
  • 打赏
  • 举报
回复
哦,关闭我会,就datain.close();dataout.close();等等
flush()怎么用,先关闭还是先刷新?请楼上两位给我讲讲,谢谢阿
jixingzhong 2005-07-14
  • 打赏
  • 举报
回复
刷新一下
myth822 2005-07-14
  • 打赏
  • 举报
回复
数据传递完后有关闭你的IO流,而且要flush一下
bbhere 2005-07-14
  • 打赏
  • 举报
回复
哦,解决了,不过运行client时,又出来问题
Exception in thread "main" java.lang.OutOfMemoryError
我查了API也没看懂是什么意思,楼上大哥能再给讲讲么?
import java.net.*;
import java.io.*;
public class Client1
{
public static void main(String[] args) throws IOException
{
Socket s=null;
String toServer;
try{
s=new Socket("192.0.0.168",6666); System.out.println("you have connected the server");
while(true)
{ try{
new ReceiveServerThread(s);
}catch(Exception e){s.close();} }
}catch(IOException e){System.err.println("Failed to connect");}

}
}
class ReceiveServerThread extends Thread{
private Socket s;
private DataInputStream datain;
private DataOutputStream dataout;
private BufferedReader in;
String toServer;
String fromServer;
public ReceiveServerThread(Socket socket) throws IOException {
s=socket;
datain=new DataInputStream(s.getInputStream());
dataout=new DataOutputStream(s.getOutputStream());
in=new BufferedReader(new InputStreamReader(System.in));
start();}
public void run()
{
try{
while(true){
fromServer=datain.readUTF();
System.out.println("server说:"+fromServer);
if(fromServer.equals("quit"))
{
System.out.println("Server exit");
break;
}
toServer=in.readLine();
dataout.writeUTF(toServer);
}
}catch(Exception e){}
}
}
mofeir 2005-07-14
  • 打赏
  • 举报
回复
换一个端口。
bbhere 2005-07-14
  • 打赏
  • 举报
回复
谢谢楼上大哥,不过还是不行,编译出一下错误
Server1.java:58:cannot resolve symbol
symbol:method flush()
location:class java.io.BufferedReader
in.flush();
^

Server1.java:59:cannot resolve symbol
symbol:method flush()
location:class java.io.DataInputStream
datain.flush();
^

这是怎么回事?
leon_jiang 2005-07-14
  • 打赏
  • 举报
回复
class ReceiveServerThread extends Thread{
private Socket s;
private DataInputStream datain;
private DataOutputStream dataout;
private BufferedReader in;
String toServer;
String fromServer;
public ReceiveServerThread(Socket socket) throws IOException {
s=socket;
datain=new DataInputStream(s.getInputStream());
dataout=new DataOutputStream(s.getOutputStream());
in=new BufferedReader(new InputStreamReader(System.in));
start();}
public void run()
{
try{
while(true){
fromServer=datain.readUTF();
System.out.println("server说:"+fromServer);
if(fromServer.equals("quit"))
{
System.out.println("Server exit");
break;
}
toServer=in.readLine();
dataout.writeUTF(toServer);
}
}catch(Exception e){}
finally
{
dataout.flush();
in.flush();
datain.flush();dataout.close();in.close();datain.close();
}
}
}

bbhere 2005-07-14
  • 打赏
  • 举报
回复
我不会用flush阿,谁能帮我把这个程序改一下吗?加分阿,加分阿
在线等
pegasus827 2005-07-14
  • 打赏
  • 举报
回复
先刷再关
bbhere 2005-07-14
  • 打赏
  • 举报
回复
我再等

62,614

社区成员

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

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