java - Socket服务器构建问题

标签 java sockets build

步骤 3 中列出的套接字服务器 http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html干净地构建(java版本“1.7.0_02”)并且运行时没有错误,但它退出时没有错误,而不是等待接受客户端。

更新了 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]));
 }
}

聊天服务器线程:

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();
   }
}

最佳答案

编辑:用可行的解决方案更新我的答案。

在您的 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();

}

其中9191是我编造的端口号。

执行 CharServer#main 方法会产生以下输出并保持 Activity 状态

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 ...

出于功能考虑,您还应该实现 stop() 方法。

关于java - Socket服务器构建问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9450510/

相关文章:

c# - 我可以在具有动态IP的C#中创建套接字服务器吗?

c++ - ROS C++ 项目 "include"文件夹?

python - 客户端无法连接到套接字

android - 所有项目的 Unity gradle 构建均失败

visual-studio - 构建时 Visual Studio 中输出 Pane 左列中数字的含义

java - 确定从何处加载特定类

java - 如何从 JWT 获取用户名?

java - 在未设置的 android 字体上使用 iText

java - 使用 MapReduce 在图中查找距离为 2 的节点对

java - 使用套接字在客户端-服务器应用程序中发送数据的最佳方式