[英]Failure to Accept Socket Connection in Java
I'm trying to make a simple program with a server and client, passing text strings back and forth. 我试图用服务器和客户端制作一个简单的程序,来回传递文本字符串。 I'm having trouble making the connection.
我在建立连接时遇到问题。 I have a test printing line right below the socket accept line and it never prints, so I assume the problem is there, but I'm not sure how to do a more thorough check.
我在套接字接受线的正下方有一条测试打印线,它从不打印,所以我认为问题出在那儿,但是我不确定如何进行更彻底的检查。
I have written this program in Eclipse if that makes a difference. 如果有帮助,我已经在Eclipse中编写了该程序。
This is the server: 这是服务器:
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
And this is the client: 这是客户:
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
Thanks! 谢谢!
I think I found your problem. 我想我找到了你的问题。
You should use println
instead of write
. 您应该使用
println
而不是write
。 I am quite sure the problem is that write does not send an actual line string + \\n
and therefore the server cannot read a line. 我非常确定问题是写操作不会发送实际的行
string + \\n
,因此服务器无法读取行。
I modified your example a little bit to make it easier to test and understand, but this works for me: 我对您的示例进行了一些修改,以使其更易于测试和理解,但这对我有用:
Server: 服务器:
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
Client: 客户:
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
ServerOutput: SERVEROUTPUT:
Received: scnhnmaiqh
收到:scnhnmaiqh
Received: tuussdmqqr收到:tuussdmqqr
Received: kuofypeefy收到:kuofypeefy
Received: vghsinefdi收到:vghsinefdi
Received: ysomirnfit收到:ysomirnfit
Received: lbhqjfbdio收到:lbhqjfbdio
Received: qhcguladyg收到:qhcguladyg
Received: wihrogklfi收到:wihrogklfi
Received: tipikgfvsx收到:tipikgfvsx
Received: fmpdcbtxqb收到:fmpdcbtxqqb
Received: yujtuefqft收到:yujtuefqft
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.