java - 因实现信使计划而迷失

标签 java multithreading instant messenger

我已经为此工作了一段时间,但遇到了一些困难。我已经尝试过很多谷歌搜索,但该主题的所有内容要么不是基于服务器的,要么是关于使用 API 的。我想坚持 java 包的基本使用。

我的目标是两个客户端能够来回发送消息,目前只需在 Eclipse 内的控制台中即可完成。我知道我必须在服务器中实现线程来处理两个连接,并且可能还必须为服务器编写一个协议(protocol)。

任何人都可以给我任何指示吗?非常感谢帮助

这是我到目前为止所拥有的

客户端

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

public class Client 
{
static Socket client;
static String input;
static DataInputStream in;
static DataOutputStream out;
static BufferedReader keyboard;


//constructor, instantiation.
static void Open()
{
    try 
    {
        client = new Socket("127.0.0.1", 6666);
        InputStream sin = client.getInputStream();
        OutputStream sout = client.getOutputStream();

        in = new DataInputStream(sin);
        out = new DataOutputStream(sout);
    } 
    catch (UnknownHostException e) {
        System.err.println("Don't know about host: taranis.");
        System.exit(1);
    } 
    catch (IOException e) {
        System.err.println("Couldn't get I/O for " + "the connection to: Host.");
        System.exit(1);
    }

    keyboard = new BufferedReader(new InputStreamReader(System.in));

}

//close
static void Close() throws IOException
{
    client.close();
    keyboard.close();
    in.close();
    out.close();
}



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

    Open();

    while(true) 
    {
        // wait for the user to type in something and press enter.
        input = keyboard.readLine(); 


        // send the above line to the server.
        out.writeUTF(input); 
        out.flush(); 

        // wait for the server to send a line of text.
        input = in.readUTF(); 

        System.out.println("The server was very polite. It sent me this : " + input);
        System.out.println();
    }


 }

 }

主持人

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

public class Host 
{

//static variables
static ServerSocket host;
static Socket client;
static HostProtocol hp;
static String input1, output2;
static InputStream sin;
static OutputStream sout;
static DataInputStream in;
static DataOutputStream out;

//constructor
public Host() throws IOException
{
    hp = new HostProtocol();
    Open();

     sin = client.getInputStream();
     sout = client.getOutputStream();
     in = new DataInputStream(sin);
     out = new DataOutputStream(sout);
}

//open sockets, instantiate variables
public static void Open()
{
    try 
    {
        host = new ServerSocket(6666);
    } 
    catch (IOException e) 
    {
        System.err.println("Could not listen on port: 6666.");
        System.exit(1);
    }

    try 
    {
      client = host.accept();
    } 
    catch (IOException e) 
    {
        System.err.println("Accept failed.");
        System.exit(1);
    }
}

//close method
public static void Close () throws IOException
{
    out.close();
    client.close();
    host.close();
}


//main method
public static void main(String[] args) throws IOException 
{

    Host serverhost = new Host();
    input1=in.readUTF();

}
} 

最佳答案

您的客户端还需要至少一个额外的线程,以便它可以随时接收和发送聊天消息。

在服务器端,您需要将对 accept 的调用放入循环中,以便接受多个连接。然后,对于每个新连接的客户端,您可以生成一个新线程并将从 accept 获取的套接字对象传递给它:

private class ClientThread implements Runnable {
    private Socket socket;

    public ClientThread(Socket socket) {
       this.socket = socket;
    }

    public void run() {
        // read messages from socket and send them to the other client.
    }
}

...

while(true) {
    try {
        client = host.accept();
        Thread t = new Thread(new ClientThread(client));
        t.start();
    } catch (IOException e) {
        System.err.println("Accept failed.");
        System.exit(1);
    }
}

关于java - 因实现信使计划而迷失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9655444/

相关文章:

java - 插入已排序的链表 - Java

java - 如何为组件设置坐标?

java - 使用比硬件线程更多的线程时的注意事项?

PHP jQuery MySQL 即时刷新

text - AutoHotKey:即时文本替换

java - 使用 Gson 反序列化 ImmutableList

java - Android:获取一个已经启动的定时器

python - 在python中记录多线程进程

ruby - Log4r 在 Rails 4 应用程序中实际上是线程安全的吗?

java - MongoDB 中的日期 : when inserting Date objects into Mongo database, 日期比自身早 1 天