0

我正在尝试制作一个简单的客户端-服务器网络程序。最初,我没有同时运行 Server 和 Client 对象。命令提示符只会在尝试运行程序时卡住。然后我决定使用threads. 结果是一样的; 我相信我必须在某个地方使用wait()notify()但我无法得到它。服务器需要先运行,但它必须等待incoming Socket引用才能继续。wait-and-notify我相信在实施机制之前需要在这里和那里转移一些线路。到目前为止,这是我的代码-:

package networking;
import java.net.*;
import java.io.*;
import java.util.Scanner;

class Server implements Runnable
{
        ServerSocket ss;
    Socket incoming;
    public void run()
    {
        try
        {
            ss = new ServerSocket(8189);
            incoming = ss.accept();
            OutputStream outs = incoming.getOutputStream();
            InputStream ins = incoming.getInputStream();
            Scanner in = new Scanner(ins);
            PrintWriter out = new PrintWriter(outs);
            out.println("Hello, Bye to exit");
            out.println("This is the server program");
            out.println("It will echo client stuff");
            boolean done = false;
            while(!done && in.hasNextLine())
            {
                out.println("Echo: " + in.nextLine());
                if(in.nextLine().trim().equals("Bye"))
                    done = true;
            }
            incoming.close();
        }
        catch(IOException e)
        {
            System.err.println(e.getMessage());
        }
    }
}

class Client implements Runnable
{
    Socket s;
    public void run()
    {
        try
        {
            s = new Socket("localhost", 8189);
            InputStream ins = s.getInputStream();
            OutputStream outs = s.getOutputStream();
            Scanner in = new Scanner(ins);
            PrintWriter out = new PrintWriter(outs);
            while(in.hasNextLine())
                System.out.println("Client: " + in.nextLine());
            out.println("Bye");
            s.close();
        }
        catch(IOException e)
        {
            System.err.println(e.getMessage());
        }
    }
}

public class Networking
{
    public static void main(String... args)
    {
        Thread server = new Thread(new Server());
        Thread client = new Thread(new Client());
        server.start();
        client.start();
    }
}

任何提示和指示将不胜感激;我只需要朝正确的方向点头(或更多)。

4

1 回答 1

0

您打开服务和客户端的代码是正确的。但是问题在于在读取或写入数据的while循环中它陷入了死锁。因为在建立连接之后,booth server 和 client 正在等待对方在流中写入一些东西。试试这个。

class Server implements Runnable {

    ServerSocket ss;
    Socket incoming;

    public void run() {
        try {
            System.out.println("Server STarted");
            ss = new ServerSocket(8189);
            incoming = ss.accept();
            System.out.println("Client accepted");
            OutputStream outs = incoming.getOutputStream();
            InputStream ins = incoming.getInputStream();
            Scanner in = new Scanner(ins);
            PrintWriter out = new PrintWriter(outs);
            out.println("Hello, Bye to exit");
            out.println("This is the server program");
            out.println("It will echo client stuff");
            boolean done = false;
            while (!done) { // && in.hasNextLine()
//              out.println("Echo: " + in.nextLine());
//              if (in.nextLine().trim().equals("Bye")) {
//                  done = true;
//              }
                out.println("TEsting from server");
            }
            incoming.close();
            System.out.println("End server");
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }
}
于 2013-09-30T06:44:09.847 回答