首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java套接字编程问题

Java套接字编程问题
EN

Stack Overflow用户
提问于 2011-03-22 20:18:27
回答 3查看 868关注 0票数 0

嘿,我正在试着运行这个套接字编程代码。

这是服务器端的代码-

代码语言:javascript
运行
复制
package sockettest;    

import java.net.*;    
import java.io.*;     

public class Server {    
public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;   
try 
{  
  serverSocket = new ServerSocket(139);   
} 
catch (IOException e) 
{  
  System.err.println("not able to listen on port");  
  System.exit(1);  
}

Socket clientSocket = null;   
try 
{
   clientSocket = serverSocket.accept();    
} 
catch (IOException e) 
{   
   System.err.println("Accept failed.");   
   System.exit(1);   
}

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); // Out is Outputstream is used to write to the Client .   
BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); // in is used to read the Client's input. 
String inputLine, outputLine;     

out.println("Hey! . Who are you?"); // Writes to client as "Hey! . Who are you?"

while ((inputLine = in.readLine()) != null)         
{
 // Reads the input from the Client. if it is "bye" the program ends.

   if (inputLine.equalsIgnoreCase("Bye"))   
   {    
       out.println("Bye");   
       break;   
   }   
   else    
   {    
      out.println("Hello Mr. " + inputLine);    
   }    
 }     
 out.close();    
 in.close();     
 clientSocket.close();     
 serverSocket.close();     
}     
}    

这是在客户端运行的代码-

代码语言:javascript
运行
复制
import java.io.*;    
import java.net.*;     

public class Client 
{      
   public static void main(String[] args) throws IOException {     

Socket kkSocket = null;     
PrintWriter out = null;     
BufferedReader in = null;     

try 
{     
   kkSocket = new Socket("192.168.2.3", 139);     
   out = new PrintWriter(kkSocket.getOutputStream(), true); // Out may be used to write to server from the client       
   in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); // in will be used to read the lines sent by the Server.      
} 
catch (UnknownHostException e)
{     
   System.err.println("Unidentified host.");     
   System.exit(1);     
}
catch (IOException e) 
{          
   System.err.println("Couldn't get I/O for the connection to.");      
   System.exit(1);      
}      


BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));     
String fromServer;     
String fromClient;      

while ((fromServer = in.readLine()) != null) {      
System.out.println("Server: " + fromServer);      
if (fromServer.equals("Bye"))     
break;     

fromClient = stdIn.readLine();     
if (fromClient != null) {      
System.out.println("Client: " + fromClient);      
out.println(fromClient);       
}      

}       


out.close();      
 in.close();      
stdIn.close();       
kkSocket.close();
}     
}    

我在客户端和服务器端运行eclipse上的代码。在cmd提示符下使用netstat -an命令,我可以看到客户机和服务器之间已经建立了连接,但是我无法通信,eclipse也没有显示任何输出。出什么问题了??

EN

回答 3

Stack Overflow用户

发布于 2011-03-22 20:21:08

你还没有告诉我们问题出在哪里。

票数 0
EN

Stack Overflow用户

发布于 2011-03-22 20:23:52

此外,您的服务器代码缺少inputLine的初始化,例如

String输入行= "";

在while循环之前

票数 0
EN

Stack Overflow用户

发布于 2011-03-22 20:40:23

请记住,如果您进行读或写操作,套接字将被阻塞。

您的客户端一直在读取,因为它会等待服务器上的每个信息,直到它为空

而且您的服务器也一直在读取并等待任何输入。

因此,只要服务器和客户端都在等待输入,就没有人会收到任何数据。

试着想出一种在服务器和客户端之间通信的协议。e.g

代码语言:javascript
运行
复制
Sever to Client: Hello Who are you?
Client receives Data and replies: Client
Server receives Information: You Are now authorized, what ya gonna do?
and so on ^^

此外,还需要out.flush()来发送消息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5391075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档