在 Java 中无法接受套接字连接

Failure to Accept Socket Connection in Java

我正在尝试用服务器和客户端制作一个简单的程序,来回传递文本字符串。我无法建立联系。我在套接字接受线下方有一条测试打印线,但它从不打印,所以我认为问题出在那里,但我不确定如何进行更彻底的检查。

我已经在 Eclipse 中编写了这个程序,如果有区别的话。

这是服务器:

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

public class HW2Q1S {

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

    try {
        //connection
        ServerSocket srvr = new ServerSocket(7654);
        Socket skt = srvr.accept();
        System.out.println(skt.getPort());

        //data xfer
        BufferedReader sIn = new BufferedReader(new InputStreamReader(skt.getInputStream()));
        PrintWriter sOut = new PrintWriter(skt.getOutputStream(), true);

        //string receiving
        int count = 1;
        String msg = "";

        while((msg = sIn.readLine()) != null) {
            while(count < 11) {
                msg = sIn.readLine();
                System.out.println("Received: "+ msg);
                String returnMsg = msg.toUpperCase();
                System.out.println("Capped: "+ returnMsg);
                sOut.write(returnMsg);
                count++;
            } 
        } //end of read from client in while loop
        if (count == 10) {
            System.out.println("Max reached.");
        }
        srvr.close();
        return;
    }

    catch(Exception e) {
        System.out.println("Error caught: " + e);
    }

} // end of main
} // end of class

这是客户:

import java.util.Random;
import java.io.*;
import java.net.*;

public class HW2Q1C {

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

    String capped = "";
    String temp = "";

    try {
        //make the connection
        Socket skt = new Socket("localhost", 7654);
        BufferedReader cIn = new BufferedReader(new InputStreamReader(skt.getInputStream()));
        PrintWriter cOut = new PrintWriter(skt.getOutputStream(), true);

        //send 11 strings
        for (int i = 0; i < 11; i++) {
            temp = Stringer();
            cOut.write(temp);
            System.out.println("Sending: " + temp);

        }

        //receive server strings
        while(cIn.readLine() != null) {
        capped = cIn.readLine();
        System.out.println("From server: "+ capped);
        }

        skt.close();
    } // end of connection try block

    catch(Exception e) {
        System.out.print("Whoops! It didn't work!\n");
    }

} //end of main

static String Stringer() {
    String msg, alpha;
    msg = "";
    alpha = "abcdefghijklmnopqrstuvwxyz";
    Random rnd = new Random();
    for (int i = 0; i < 10; i++) {
        msg += alpha.charAt(rnd.nextInt(25));
    }
    return msg;
}
 } //end of class

谢谢!

我想我发现了你的问题。
您应该使用 println 而不是 write。我很确定问题是 write 不发送实际行 string + \n 因此服务器无法读取行。
我稍微修改了您的示例以使其更易于测试和理解,但这对我有用:

服务器:

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

public class Server {
    public static void main(String[] args) throws Exception {
        try {
            //connection
            ServerSocket srvr = new ServerSocket(7654);
            Socket skt = srvr.accept();
            System.out.println(skt.getPort());

            BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));

            String msg = "";
            while ((msg = in.readLine()) != null) {
                System.out.println("Received: " + msg);
            } //end of read from client in while loop
            srvr.close();
        } catch (Exception e) {
            System.out.println("Error caught: " + e);
        }

    } // end of main
} // end of class

客户:

import java.util.Random;
import java.io.*;
import java.net.*;

public class Client {

    public static void main(String[] args) throws IOException {
        try {
            Socket socket = new Socket("localhost", 7654);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            for (int i = 0; i < 11; i++) {
                out.println(Stringer()); //<-- println instead of write
            }
            socket.close();
        } // end of connection try block
        catch(Exception e) {
            System.out.print(e.toString());
        }

    } //end of main

    static String Stringer() {
        String msg, alpha;
        msg = "";
        alpha = "abcdefghijklmnopqrstuvwxyz";
        Random rnd = new Random();
        for (int i = 0; i < 10; i++) {
            msg += alpha.charAt(rnd.nextInt(25));
        }
        return msg;
    }
} //end of class

服务器输出:

Received: scnhnmaiqh
Received: tuussdmqqr
Received: kuofypeefy
Received: vghsinefdi
Received: ysomirnfit
Received: lbhqjfbdio
Received: qhcguladyg
Received: wihrogklfi
Received: tipikgfvsx
Received: fmpdcbtxqb
Received: yujtuefqft