java 从面向连接(TCP)客户端/服务器的服务器套接字读取客户端套接字上的字节数据包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3665117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read the packets of bytes on client socket from server socket for connection oriented (TCP) client/server?
提问by user441507
i m creating connection oriented server/client(TCP) socket.i have created whole server socket and i have written packet on server socket successfully and i have created client socket also but i m not be able to read packet so please give me the idea about read the packet(code or example) on client socket and tell clearly that can i read a packet on client socket or not if no then what should use in place of client and server socket
我正在创建面向连接的服务器/客户端(TCP)套接字。我已经创建了整个服务器套接字,我已经成功地在服务器套接字上写入了数据包,我也创建了客户端套接字,但我无法读取数据包,所以请给我关于读取的想法客户端套接字上的数据包(代码或示例)并清楚地告诉我是否可以在客户端套接字上读取数据包,如果没有,那么应该使用什么来代替客户端和服务器套接字
回答by Jon Skeet
You don't generally read a packet at a time - you read from the InputStream
returned by Socket.getInputStream()
. You should almost certainly be treating the connection as a stream, rather than even attempting to handle individual packets.
您通常不会一次读取一个数据包 - 您从InputStream
返回的Socket.getInputStream()
. 您几乎肯定应该将连接视为流,而不是尝试处理单个数据包。
If you still run into problems, it would really help if you could post some code to show how you're connecting the socket etc.
如果您仍然遇到问题,如果您可以发布一些代码来显示您如何连接套接字等,那将非常有帮助。
回答by Sanchit
I had to implement such a thing for my networking course in college.
我不得不在大学的网络课程中实现这样的东西。
Here's an excerpt from the book
这是本书的摘录
TCPServer.java
TCPServer.java
import java.io.*;
import java.net.*;
class TCPServer
{
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
TCPClient.java
客户端程序
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
Hopefully this will be sufficient to figure out what you did wrong. Good luck!
希望这足以找出你做错了什么。祝你好运!