首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

socket通信程序中的有关问题

2011-12-27 
socket通信程序中的问题下面是两段程序,服务器端的和客户端的,现在的问题是:当服务器端给客户端发数据之后

socket通信程序中的问题
下面是两段程序,服务器端的和客户端的,现在的问题是:
当服务器端给客户端发数据之后,客户端好像接受不到,如果把//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);
}

}
}


[解决办法]
大概看了下,应该是客户端和服务端都在等待接收数据,结果是死等在那里了。
应该是一方收到信息后要发出些东西让对方接收,再进行判断,要不就只作接收!
[解决办法]
呵呵
好难看呀
我以前也遇到这样的问题过
后来在客户端与服务器端都重新启动了一个线程以后就解决了
[解决办法]
服务器端接收到请求就将消息返回给客户端,你这个input、input1有点乱,你能细说一下不
[解决办法]
楼主试下在客户端开个线程去accept服务器的响应呢?好像没有这个唉.

热点排行
Bad Request.