ServerSocket java-server只读取一次输入?

我写了一个java服务器,这里是代码:

try { ss = new ServerSocket(8080); while (true) { socket = ss.accept(); System.out.println("Acess given"); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //out = new PrintWriter(socket.getOutputStream(),true); line = in.readLine(); System.out.println("you input is :" + in.readLine()); } } 

一个iphone应用程序是客户端,它有代码:

 - (void)viewDidLoad { [super viewDidLoad]; socket = [[LXSocket alloc]init]; if ([socket connect:@"10.211.55.2" port:8080]) { NSLog(@"socket has been created"); } else { NSLog(@"socket couldn't be created created"); } @try { }@catch (NSException * e) { NSLog(@"Unable to send data"); } [super viewDidLoad]; } -(IBAction)sendData{ [socket sendString:@"A\n"]; } 

我在这里有两个问题:首先是服务器只读取输入一次。 第二个是,当我尝试输出数据时,它不会输出,直到我调用该方法两次(点击uibutton两次)。 不知道这里发生了什么。 我究竟做错了什么?

您每次在while循环中创建一个新的阅读器。 而是将代码移到while循环之外并阻塞readLine()调用。

 socket = ss.accept(); in = new BufferedReader(new InputStreamReader(socket.getInputStream()); String line = ""; while ( true) { line = in.readLine(); System.out.println("you input is :" + line); if ( "Bye".equals(line) ) break; } 

这是一个示例服务器端程序。

由于alphazero发布了模式,我将发布一个简短的简化实现:

这是服务器:

 try { ServerSocket ss = new ServerSocket(portNumber); logger.info("Server successfully started on port " + portNumber); // infinite loop that waits for connections while (true) { SocketThread rst = new SocketThread(ss.accept()); rst.start(); } } catch (IOException e) { logger.info("Error: unable to bind to port " + portNumber); System.exit(-1); } 

SocketThread类似于:

 public class SocketThread extends Thread { private Socket communicationSocket = null; public SocketThread(Socket clientSocket) { communicationSocket = clientSocket; try { input = new ObjectInputStream(communicationSocket.getInputStream()); } catch (IOException e) { logger.info("Error getting communication streams to transfer data."); try { communicationSocket.close(); } catch (IOException e1) { e1.printStackTrace(); } } } public void run() { boolean listening=true; DataObject command = null; while (listening) { try { Object currentObject = input.readObject(); if (currentObject != null && currentObject instanceof DataObject) { command = (DataObject) currentObject; } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { // If we got to this point is because we received a request from // the client // we can exit the loop listening = false; } } } } 

注意:“DataObject”只是一个自定义类,它可以更实用,因为您可以从套接字读取Dataobject本身,而不必担心您正在读取多少字节等。只有条件是DataObject被标记为Serializable。

希望能帮助到你。

图莎尔,

一般模式是这样的(几乎是java但是伪代码):

 while (server-socket is accepting new connections) { // The server-socket's job is to listen for connection requests // It does this typically in a loop (until you issue server-shutdown) // on accept the server-socket returns a Socket to the newly connected client // socket s = server-socket.accept-connection(); // various options here: // // typically fire off a dedicated thread to servie this client // but also review NIO or (home-grown) connection-map/handler patterns // the general pattern: // create a dedicated thread per connection accepted. // pass Socket (s) to the handler method (a Runnable) and start it off // and that is it. // Here we use the general pattern and create a dedicated // handler thread and pass of the new connections' socket reference // Thread handler-thread = new Thread (handler-routine-as-runnable, s); handler-thread.start(); }