简体   繁体   English

套接字服务器生成问题

[英]Socket server build issue

The socket server listed in step 3 from http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html builds cleanly (java version "1.7.0_02") and runs without error but it exits without error instead of waiting to accept clients. http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-c​​lient-server.html在第3步中列出的套接字服务器可以正常构建(Java版本“ 1.7.0_02”),并且无需运行错误,但是它退出时没有错误,而不是等待接受客户端。

Updated ChatServer with missing arg code: 更新了缺少arg代码的ChatServer:

ChatServer: 聊天服务器:

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

public class ChatServer implements Runnable
{  private ServerSocket     server = null;
   private Thread           thread = null;
   private ChatServerThread client = null;

   public ChatServer(int port)
   {  try
      {  System.out.println("Binding to port " + port + ", please wait  ...");
         server = new ServerSocket(port);
         System.out.println("Server started: " + server);
         start();
      }
      catch(IOException ioe)
      {  System.out.println(ioe); }
   }
   public void run()
   {  while (thread != null)
      {  try
         {  System.out.println("Waiting for a client ...");
            addThread(server.accept());
         }
         catch(IOException ie)
         {  System.out.println("Acceptance Error: " + ie); }
      }
   }
   public void addThread(Socket socket)
   {  System.out.println("Client accepted: " + socket);
      client = new ChatServerThread(this, socket);
      try
      {  client.open();
         client.start();
      }
      catch(IOException ioe)
      {  System.out.println("Error opening thread: " + ioe); }
   }
   public void start() {
   thread = new Thread(this);
   thread.start();
 }
   public void stop()                    { /* no change */ }
   public static void main(String args[]) {
      ChatServer server = null;
      if (args.length != 1)
         System.out.println("Usage: java ChatServer port");
      else
         server = new ChatServer(Integer.parseInt(args[0]));
 }
}

ChatServerThread: ChatServerThread:

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

public class ChatServerThread extends Thread
{  private Socket          socket   = null;
   private ChatServer      server   = null;
   private int             ID       = -1;
   private DataInputStream streamIn =  null;

   public ChatServerThread(ChatServer _server, Socket _socket)
   {  server = _server;  socket = _socket;  ID = socket.getPort();
   }
   public void run()
   {  System.out.println("Server Thread " + ID + " running.");
      while (true)
      {  try
         {  System.out.println(streamIn.readUTF());
         }
         catch(IOException ioe) {
            System.out.println(ioe.getMessage());
         }
      }
   }
   public void open() throws IOException
   {  streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
   }
   public void close() throws IOException
   {  if (socket != null)    socket.close();
      if (streamIn != null)  streamIn.close();
   }
}

EDIT: Updating my answer with a working solution. 编辑:用一个可行的解决方案更新我的答案。

Change these methods in your ChatServer class in order to be like these 像这样,在您的ChatServer类中更改这些方法

public void start() {
    thread = new Thread(this);
    thread.start();
}

public void stop() { 
    // You should implement this too
}

public static void main(String args[]) { 
    // Instantiate a CharServer with the listening port 9191
    ChatServer chatServer = new ChatServer(9191);
    // CharServer.start() should not be confused with Thread.start();
    // This calls our custom method up above, which includes a call to
    // Thread(ChatServer).start();
    chatServer.start();

}

Where 9191 is a port number I made up. 其中9191是我填写的端口号。

Executing CharServer #main method produces the following output and stays alive 执行CharServer #main方法将产生以下输出并保持活动状态

Binding to port 9191, please wait  ...
Server started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=9191]
Waiting for a client ...
Waiting for a client ...

You should also implement stop() method for the sake of functionality. 为了功能起见,您还应该实现stop()方法。

 {  while (thread != null)

You never set thread so it will all be null And you never create a thread 您永远不会设置线程,因此所有线程都将为空,并且永远不会创建线程

Try changing start() to: 尝试将start()更改为:

public void start()                   { 
   thread = new Thread(this);
   thread.start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM