我正在尝试使用服务器和客户端制作一个简单的程序,来回传递文本字符串。我无法建立连接。我在套接字接受线正下方有一条测试打印线,但它从不打印,所以我认为问题就在那里,但我不确定如何进行更彻底的检查。
如果这有什么不同的话,我已经在 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
关于java - Java中接受Socket连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42427022/