socket通信程序中的问题

hewenhao 2007-09-05 04:55:03
下面是两段程序,服务器端的和客户端的,现在的问题是:
当服务器端给客户端发数据之后,客户端好像接受不到,如果把//code for test之间的代码去掉,就可以实现客户端到服务器端的数据传送,难道逆向就不能了吗?


服务器段程序:
import java.io.*;
import java.net.*;

public class tcpServer {
public static void main(String[] args)
{
int port=1500;
ServerSocket server_socket;
BufferedReader input;
//code for test
BufferedReader input1;
PrintWriter output;
String lineToSend;
//code for test
try
{
server_socket=new ServerSocket(port);
System.out.println("server listening on port:"+server_socket.getLocalPort());

while(true)
{
Socket socket=server_socket.accept();
System.out.println("New connection at "+socket.getInetAddress()+":"+socket.getPort());
input=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//code for test
input1=new BufferedReader(new InputStreamReader(System.in));
output=new PrintWriter(socket.getOutputStream());
//code for test
try
{
while(true)
{

//code for test
lineToSend=input1.readLine();
System.out.println(lineToSend);
if(lineToSend.equals("end"))
{
System.out.println("game over");
break;
}
System.out.println("begin");
output.println(lineToSend);
System.out.println("end");
//code for test

String message=input.readLine();

if(message==null)
{
System.out.println("message equals null");
break;
}

System.out.println(message);

}
}
catch(IOException e)
{
System.out.println(e);
}

try
{
socket.close();
System.out.println("socket closed by client");
}
catch(IOException e)
{
System.out.println(e);
}
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}

客户端程序:
import java.io.*;
import java.net.*;

public class tcpClient
{
public static void main(String[] args)
{
int port=1500;
String server="localhost";
Socket socket=null;
String lineToSend;
BufferedReader input;
PrintWriter output;
int ERROR=1;
//code for test
BufferedReader input1;
//code for test

//connect to the server
try
{
socket=new Socket(server,port);
System.out.println("connected with server "+socket.getInetAddress()+":"+socket.getPort());
}
catch(UnknownHostException e)
{
System.out.println(e);
System.exit(ERROR);
}
catch(IOException e)
{
System.out.println(e);
System.exit(ERROR);
}

try
{
input=new BufferedReader(new InputStreamReader(System.in));
output=new PrintWriter(socket.getOutputStream(),true);
//code for test
input1=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//code for test
while(true)
{

//code for test
System.out.println("begin");
String message=input1.readLine();
System.out.println("end");
if(message==null)
{
System.out.println("message equals null");
break;
}
System.out.println(message);
//code for test

lineToSend=input.readLine();

if(lineToSend.equals("end"))
{
System.out.println("game over");
break;
}
output.println(lineToSend);

}
}
catch(IOException e)
{
System.out.println(e);
}

try
{
socket.close();
System.out.println("socket closed");
}
catch(IOException e)
{
System.out.println(e);
}

}
}
...全文
237 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hewenhao 2007-09-07
  • 打赏
  • 举报
回复
是不是只有accept响应的那一段能接收数据啊?
daimon0316 2007-09-06
  • 打赏
  • 举报
回复
服务器端接收到请求就将消息返回给客户端,你这个input、input1有点乱,你能细说一下不
youbin_ 2007-09-06
  • 打赏
  • 举报
回复
如果不用交互就不用!
hewenhao 2007-09-06
  • 打赏
  • 举报
回复
必须采用多线程才能解决这个问题,是吗?
hupo1007 2007-09-06
  • 打赏
  • 举报
回复
楼主试下在客户端开个线程去accept服务器的响应呢?好像没有这个唉.
lfcai 2007-09-06
  • 打赏
  • 举报
回复
呵呵
好难看呀
我以前也遇到这样的问题过
后来在客户端与服务器端都重新启动了一个线程以后就解决了
youbin_ 2007-09-06
  • 打赏
  • 举报
回复
大概看了下,应该是客户端和服务端都在等待接收数据,结果是死等在那里了。
应该是一方收到信息后要发出些东西让对方接收,再进行判断,要不就只作接收!
hewenhao 2007-09-06
  • 打赏
  • 举报
回复
哪位大哥帮忙看看啊,我谢你!
hewenhao 2007-09-06
  • 打赏
  • 举报
回复
自己顶一下!
hewenhao 2007-09-06
  • 打赏
  • 举报
回复
为什么我从服务器端发数据,客户端接收不到呢?改写的程序如下:
服务器端:
import java.io.*;
import java.net.*;

public class tcpServer {
public static void main(String[] args)
{
int port=3024;
ServerSocket server_socket;
BufferedReader input;
PrintWriter output;
String lineToSend;
try
{
server_socket=new ServerSocket(port);
System.out.println("server listening on port:"+server_socket.getLocalPort());


while(true)
{
Socket socket=server_socket.accept();
System.out.println("New connection at "+socket.getInetAddress()+":"+socket.getPort());
input=new BufferedReader(new InputStreamReader(System.in));
output=new PrintWriter(socket.getOutputStream());
try
{
while(true)
{
lineToSend=input.readLine();
System.out.println(lineToSend);
if(lineToSend.equals("end"))
break;
output.print(lineToSend);
}
}
catch(IOException e)
{
System.out.println(e);
}

try
{
socket.close();
System.out.println("socket closed by client");
}
catch(IOException e)
{
System.out.println(e);
}

}//end while
}
catch(IOException e)
{
System.out.println(e);
}
}
}

客户端:
import java.io.*;
import java.net.*;

public class tcpClient
{
public static void main(String[] args)
{
int port=3024;
String server="localhost";
Socket socket=null;
BufferedReader input;
int ERROR=1;
//connect to the server
try
{
socket=new Socket(server,port);
System.out.println("connected with server "+socket.getInetAddress()+":"+socket.getPort());
}
catch(UnknownHostException e)
{
System.out.println(e);
System.exit(ERROR);
}
catch(IOException e)
{
System.out.println(e);
System.exit(ERROR);
}

try
{
input=new BufferedReader(new InputStreamReader(socket.getInputStream()));

while(true)
{
System.out.println("begin");
String message=input.readLine();
System.out.println("end");
if(message==null)
break;
System.out.println(message);
}
}
catch(IOException e)
{
System.out.println(e);
}

try
{
socket.close();
System.out.println("socket closed");
}
catch(IOException e)
{
System.out.println(e);
}

}
}
daimon0316 2007-09-06
  • 打赏
  • 举报
回复
双向通信完全可以呀,互为服务端嘛,起两个端口,现在好多短消息网关就是这个样子
youbin_ 2007-09-06
  • 打赏
  • 举报
回复
可以双向的啊,就是用一种交互的机制,不能同时都等在那里
hewenhao 2007-09-06
  • 打赏
  • 举报
回复
数据只能从客户端向服务器端发送吗?

62,623

社区成员

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

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