JAVA STOCKET 的问题

Eniak 2010-03-05 08:25:14
自己根据网上一个例子,改了一个为 CS 结构的程序,想实现一问一答的通信效果。出现了两个问题

1 我在客户端第一次输入的时候,可以正常的显示,但是第二次在输入一个的时候,程序就断了,说有毛病。

2 如果前一次非正常退出的话,那么第二次在启动程序的时候,就会有问题了,Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind
我就会用 ctrl + alt + del 找到一个 javaw.exe 东东, 把它关掉,就行了

请问,这些问题该怎么解决呢??

好吧,上代码



// 服务器端

public class Server {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(12345);
System.out.println("The server is started, please start the client");

boolean flag = true;
byte[] buff = new byte[1024];

while( flag ) {
Socket so = s.accept();
int read = so.getInputStream().read(buff);
String ss = new String(buff,0,read);
if( ss.equals("end"))
so.getOutputStream().write("end".getBytes());
so.getOutputStream().write("respose".getBytes());
so.getOutputStream().flush();
so.close();
}
s.close();
}
}




//客户端
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("please input the length of (3,4,5)");
String ipaddr = "localhost";
Socket so = new Socket(ipaddr,12345);

boolean flag = true;

while ( flag ) {
System.out.println("Please input something here");

byte[] buff=new byte[64];
int r = System.in.read(buff);

so.getOutputStream().write(new String(buff,0,r).getBytes());
System.out.println("we got back from Server");
r = so.getInputStream().read(buff); //这行出错了
String str = new String(buff,0,r);
System.out.print( str );
if( str.equals("end"))
flag = false;
}
so.close();

}

}
...全文
180 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyangdesky 2010-03-09
  • 打赏
  • 举报
回复
支持一下啊!!!!!
liguang168 2010-03-09
  • 打赏
  • 举报
回复
引用 8 楼 eniak 的回复:
我在网上也看过你这种做法,感觉就是一个服务器对应多个客户端的程序,用到了多线程。

但是现在这个程序的问题出现在,一个客户端和一个服务器多次通信中出现了问题,在第二次有客户端发送信息时,出现了问题,问题在

r = so.getInputStream().read(buff);    //这行出错了


引用 4 楼 liguang168 的回复:客户端没有改你的. 服务端改了一下.你自己看了再慢慢改吧.这已经是很小的DEMO了. public class Server { public static void main(String[] args) throws IOException { ServerSocket s = new ServerSocket(12345); System.out.println("The server is started, please start the client"); boolean flag = true; while (flag) { Socket so = s.accept(); new SocketServer(so).start(); } s.close(); } } class SocketServer extends Thread { Socket socket; SocketServer(Socket socket) { this.socket = socket; } public void run() { try { byte[] buff = new byte[1024]; while (true) { int read = socket.getInputStream().read(buff); System.out.println(new String(buff)); String ss = new String(buff, 0, read); if (ss.equals("end")) { socket.getOutputStream().write("end".getBytes()); socket.getOutputStream().flush(); break; } else { socket.getOutputStream().write("respose".getBytes()); socket.getOutputStream().flush(); } } } catch (Exception ex) { ex.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }


当客户端退出时,肯定会发生异常。你在catch块中进行处理。Socket有一个断开连接判断。
我只是模拟一下,那个new SocketServer(so).start(); 即一个客户端。
你可用List来管理。实现的话,还是你先慢慢捉摸吧。
huhk 2010-03-06
  • 打赏
  • 举报
回复
出现异常后没关闭线程,你的port被占用,再开程序会端口冲突。
hjh811 2010-03-06
  • 打赏
  • 举报
回复
正如5楼所说,你不正常退出,应该是端口被占用的问题。运行错误后再换个端口运行试试就知了
coolbamboo2008 2010-03-06
  • 打赏
  • 举报
回复
同意5楼,最好是捕捉异常处理
zoutuo 2010-03-06
  • 打赏
  • 举报
回复
标题写错了 回复内容太短了!
Eniak 2010-03-06
  • 打赏
  • 举报
回复
改了一下,好像每次通信,都要从新建立一个socket,这样子才能连接成,客户端不会有异常
Eniak 2010-03-06
  • 打赏
  • 举报
回复
我在网上也看过你这种做法,感觉就是一个服务器对应多个客户端的程序,用到了多线程。

但是现在这个程序的问题出现在,一个客户端和一个服务器多次通信中出现了问题,在第二次有客户端发送信息时,出现了问题,问题在

r = so.getInputStream().read(buff); //这行出错了


引用 4 楼 liguang168 的回复:
客户端没有改你的.

服务端改了一下.你自己看了再慢慢改吧.这已经是很小的DEMO了.
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(12345);
System.out.println("The server is started, please start the client");

boolean flag = true;

while (flag) {
Socket so = s.accept();
new SocketServer(so).start();
}
s.close();
}
}

class SocketServer extends Thread {
Socket socket;

SocketServer(Socket socket) {
this.socket = socket;
}

public void run() {

try {
byte[] buff = new byte[1024];

while (true) {
int read = socket.getInputStream().read(buff);
System.out.println(new String(buff));
String ss = new String(buff, 0, read);

if (ss.equals("end")) {
socket.getOutputStream().write("end".getBytes());
socket.getOutputStream().flush();
break;
} else {
socket.getOutputStream().write("respose".getBytes());
socket.getOutputStream().flush();
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
liguang168 2010-03-05
  • 打赏
  • 举报
回复
客户端没有改你的.

服务端改了一下.你自己看了再慢慢改吧.这已经是很小的DEMO了.
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(12345);
System.out.println("The server is started, please start the client");

boolean flag = true;

while (flag) {
Socket so = s.accept();
new SocketServer(so).start();
}
s.close();
}
}

class SocketServer extends Thread {
Socket socket;

SocketServer(Socket socket) {
this.socket = socket;
}

public void run() {

try {
byte[] buff = new byte[1024];

while (true) {
int read = socket.getInputStream().read(buff);
System.out.println(new String(buff));
String ss = new String(buff, 0, read);

if (ss.equals("end")) {
socket.getOutputStream().write("end".getBytes());
socket.getOutputStream().flush();
break;
} else {
socket.getOutputStream().write("respose".getBytes());
socket.getOutputStream().flush();
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
liguang168 2010-03-05
  • 打赏
  • 举报
回复
先说服务端吧:

ServerSocket s = new ServerSocket(12345);
System.out.println("The server is started, please start the client");

boolean flag = true;
byte[] buff = new byte[1024];

while( flag ) {
// 监听客户端的连接,怎么放在这里? 用另一个循环来实现.
Socket so = s.accept();

// 这个接受数据又需要用另一个循环来实现.
int read = so.getInputStream().read(buff);
String ss = new String(buff,0,read);

// 这个不规范.
if( ss.equals("end"))
so.getOutputStream().write("end".getBytes());
so.getOutputStream().write("respose".getBytes());
so.getOutputStream().flush();

so.close();
}
s.close();
}
YangMacgrady 2010-03-05
  • 打赏
  • 举报
回复
以前跟着写过,也遇到过这样的问题,但是忘了怎么解决的,我是菜鸟
stu202060510 2010-03-05
  • 打赏
  • 举报
回复
不会,顶一下楼主。。。

62,614

社区成员

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

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