简体   繁体   English

使用java中的套接字创建简单的客户端/代理/服务器程序

[英]Making simple client/proxy/server programs using sockets in java

I'm working on an assignment where I have to make 3 programs, a client, a proxy, and a server. 我正在完成一项任务,我必须制作3个程序,一个客户端,一个代理和一个服务器。 The client is supposed to send messages to the server by sending them to the proxy, which sends them to the server, and vice versa. 客户端应该通过将消息发送到代理来向服务器发送消息,代理将消息发送到服务器,反之亦然。 The server waits for the proxy to connect to it, and the proxy waits for the client to connect to it, then the client sends a message to the proxy which gets sent to the server. 服务器等待代理连接到它,并且代理等待客户端连接到它,然后客户端向代理发送消息并将其发送到服务器。 When I compile them though, while the proxy gets the message fine, the serve gets an error. 当我编译它们时,虽然代理获得了正确的消息,但是服务器会出错。

Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at Server.run(Server.java:18)
at Server.main(Server.java:8)

Here is the client code 这是客户端代码

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


public class Client {

public static void main(String[] args) throws Exception{
    Client serv = new Client();
    serv.run();
    }

    public void run() throws Exception{
    Socket sock = new Socket("localhost", 7777);
    PrintStream ps = new PrintStream(sock.getOutputStream());
    ps.println("Hello Server");
    sock.close();
    }

}

Here is the proxy, 这是代理,

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

public class Proxy {

public static void main (String[] args) throws Exception{
    Proxy serv = new Proxy();
    serv.run();
}

    public void run() throws Exception{
    ServerSocket ss = new ServerSocket(7777);
    Socket sock = ss.accept();

    InputStreamReader ir = new InputStreamReader(sock.getInputStream());
    BufferedReader br = new BufferedReader(ir);

    String message = "";
    message = br.readLine();
    System.out.println(message);

    Socket sck = new Socket("localhost", 8888);
    PrintStream ps = new PrintStream(sock.getOutputStream());

    boolean waiting = true;
    while(waiting){
        if(message != null){
        ps.println("Hey Server, Client says: " + message);
        waiting = false;
        }
    }
    sock.close();
    }

}

and here is the server 这是服务器

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

public class Server {

    public static void main (String[] args) throws Exception{
    Server serv = new Server();
    serv.run();
    }

    public void run() throws Exception{
    ServerSocket ss = new ServerSocket(8888);
    Socket sock = ss.accept();

    InputStreamReader ir = new InputStreamReader(sock.getInputStream());
    BufferedReader br = new BufferedReader(ir);

    System.out.println(br.readLine());
    sock.close();
    ss.close();
    }

}

Does anybody know what I'm doing wrong? 有人知道我做错了什么吗?

Look at your proxy. 看看你的代理。 First the sock Socket represents the connection with the client, then the sck Socket represents the connection with the server. 首先sock Socket表示与客户端的连接,然后sck Socket表示与服务器的连接。 However, when you create your PrintStream ps to connect with the server, you are using sock.getOutputStream as opposed to sck.getOutputStream . 但是,当您创建PrintStream ps以与服务器连接时,您使用sock.getOutputStream而不是sck.getOutputStream You are telling it to send information back to the client, which does not have an open BufferedReader to interpret, nor an open Socket at that point. 您告诉它将信息发送客户端,该客户端没有要解释的打开的BufferedReader,也没有打开的Socket。 Attempting to write to a closed connection results in a connection reset exception. 尝试写入已关闭的连接会导致连接重置异常。

Secondly (though this is not causing the error), your proxy while loop is all jacked up. 其次(虽然这不会导致错误),您的代理while循环全部被抢占。 If you examine it, it will obviously cycle through at least once because waiting is defaulted to true. 如果你检查它,它显然会循环至少一次因为等待默认为真。 Then, you are checking to see if message != null . 然后,您正在检查是否message != null In this case, message will never be null because the client is set to always send the same message, and only one message. 在这种情况下, message永远不会为空,因为客户端设置为始终发送相同的消息,并且只发送一条消息。 Most importantly, however, if for whatever reason message was null, there is nothing in the while loop to change that -- it does not attempt to read the next value from br or anything, so you will simply be stuck in an infinite while loop. 然而,最重要的是,如果由于某种原因message null,则while循环中没有任何内容可以改变它 - 它不会尝试从br或任何内容中读取下一个值,因此您将只是陷入无限循环中。

I believe what you were looking for was this: 我相信你要找的是这个:

String message;
while((message = br.readLine()) != null){
    ps.println("Hey server, Client says: " + message);
}

But as I said, a while loop is not necessary in this case because the client is set to send only one message anyways, so you can simply do this: 但正如我所说,在这种情况下不需要while循环,因为客户端设置为只发送一条消息,所以你可以简单地这样做:

ps.println("Hey server, Client says: " + message);

... without any sort of looping. ......没有任何循环。

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

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