解决TCP客户端和服务器端通信读不到数据的问题
解决:
服务器端和客户端读完后加上client.shutdownInput();
服务器端和客户端写完后加上client.shutdownOutput();
服务器端代码:
public class TCPTestServer2 {
public static void main(String[] args) throws IOException {
System.out.println("---------------服务器--------------");
ServerSocket server = new ServerSocket(9999);
Socket client = server.accept();
System.out.println("有一个用户连接。。。。。。。");
InputStream is = client.getInputStream();
byte[] msg = is.readAllBytes();
System.out.println(new String(msg));
is.close();
client.close();
server.close();
}
}
客户端代码:
public class TCPTestClient2 {
public static void main(String[] args) throws IOException {
System.out.println("-------------客户端------------");
Socket client = new Socket("127.0.0.1", 9999);
Scanner sc = new Scanner(System.in);
byte[] msg = sc.nextLine().getBytes();
OutputStream os = client.getOutputStream();
os.write(msg);
os.flush();
os.close();
client.close();
}
}
运行服务器端再运行客户端,在客户端中输入要发送的信息,回车
服务器收到信息,over
然后!!!!!!!!!!!!!!!
想让服务器端回信息,结果翻车了。。。。。。
翻车全过程↓
客户端代码:
加了一个
InputStream is = client.getInputStream();
System.out.println(new String(is.readAllBytes()));
用来读服务器端发过来的信息
public class TCPTestClient2 {
public static void main(String[] args) throws IOException {
System.out.println("-------------客户端------------");
Socket client = new Socket("127.0.0.1", 9999);
Scanner sc = new Scanner(System.in);
byte[] msg = sc.nextLine().getBytes();
OutputStream os = client.getOutputStream();
os.write(msg);
InputStream is = client.getInputStream();
System.out.println(new String(is.readAllBytes()));
is.close();
os.flush();
os.close();
client.close();
}
}
服务器端:
只是加了个输出流
public class TCPTestServer2 {
public static void main(String[] args) throws IOException {
System.out.println("---------------服务器--------------");
ServerSocket server = new ServerSocket(9999);
Socket client = server.accept();
System.out.println("有一个用户连接。。。。。。。");
InputStream is = client.getInputStream();
OutputStream os = client.getOutputStream();
byte[] msg = is.readAllBytes();
System.out.println(new String(msg));
Scanner sc = new Scanner(System.in);
byte[] msg2 = sc.nextLine().getBytes();
os.write(msg2);
os.flush();
os.close();
is.close();
client.close();
server.close();
}
}
运行,程序未停止运行且服务器端也没有输出,客户端发过来的消息
也就是System.out.println(new String(msg));没执行到
package homework0526.login1;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class TCPTestServer2 {
public static void main(String[] args) throws IOException {
System.out.println("---------------服务器--------------");
ServerSocket server = new ServerSocket(9999);
Socket client = server.accept();
System.out.println("有一个用户连接。。。。。。。");
InputStream is = client.getInputStream();
OutputStream os = client.getOutputStream();
byte[] msg = is.readAllBytes();
client.shutdownInput();
String s = new String(msg);
System.out.println(s);
Scanner sc = new Scanner(System.in);
byte[] msg2 = sc.nextLine().getBytes();
os.write(msg2);
client.shutdownOutput();
os.flush();
os.close();
is.close();
client.close();
server.close();
}
}
应该也许的原因:
解决:
服务器端和客户端读完后加上client.shutdownInput();
服务器端和客户端写完后加上client.shutdownOutput();
服务器端:
public class TCPTestServer2 {
public static void main(String[] args) throws IOException {
System.out.println("---------------服务器--------------");
ServerSocket server = new ServerSocket(9999);
Socket client = server.accept();
System.out.println("有一个用户连接。。。。。。。");
InputStream is = client.getInputStream();
OutputStream os = client.getOutputStream();
byte[] msg = is.readAllBytes();
client.shutdownInput();
String s = new String(msg);
System.out.println(s);
Scanner sc = new Scanner(System.in);
byte[] msg2 = sc.nextLine().getBytes();
os.write(msg2);
client.shutdownOutput();
os.flush();
os.close();
is.close();
client.close();
server.close();
}
}
客户端:文章来源:https://www.toymoban.com/news/detail-720507.html
package homework0526.login1;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class TCPTestClient2 {
public static void main(String[] args) throws IOException {
while (true) {
System.out.println("-------------客户端------------");
Socket client = new Socket("127.0.0.1", 9999);
Scanner sc = new Scanner(System.in);
byte[] msg = sc.nextLine().getBytes();
OutputStream os = client.getOutputStream();
os.write(msg);
client.shutdownOutput();
InputStream is = client.getInputStream();
System.out.println(new String(is.readAllBytes()));
client.shutdownInput();
is.close();
os.flush();
os.close();
client.close();
}
}
}
文章来源地址https://www.toymoban.com/news/detail-720507.html
到了这里,关于Java实现TCP客户端和服务器端相互通信的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!