TCP
1.服务器绑定端口号启动服务器欢迎socket。
2. 等待客户端发送请求。
3.等到客户端发送请求之后,服务器与客户端重新建立一个socket,但是客户端的目标端口不变。
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
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();
}
}
服务端
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
//开一个socket用做于欢迎socket
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();
//改变客户端发送的信息
capitalizedSentence =
clientSentence.toUpperCase() + '\n';
//发回给客户端
outToClient.writeBytes(capitalizedSentence);
}
}
}
UDP
1.udp的客户端可以比udp的服务端先开启
2.服务器绑定端口 文章来源:https://www.toymoban.com/news/detail-514540.html
java代码如下:
客户端
文章来源地址https://www.toymoban.com/news/detail-514540.html
import java.io.*;
import java.net.*;
class UDPClient {
public static void main(String args[]) throws Exception
{
BufferedReader inFromUser =
new BufferedReader(new InputStreamReader
(System.in));
//为客户端进程创建了一个门,但没有在两个进程之间创建管道
DatagramSocket clientSocket = new DatagramSocket();
//调用DNS查找
InetAddress IPAddress =
InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
//与TCP发送流不同,udp是打一个包发过去,这个就是udp的包
//这一行构造数据包sendPacket,客户机将通过套接字将其弹出到网络中。
// 这个包包括包中包含的数据、sendData、该数据的长度、服务器的IP地址和应用程序的端口号(我们将其设置为9876)。
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length,
IPAddress, 9876);
clientSocket.send(sendPacket);
//接受服务器响应包
DatagramPacket receivePacket =
new DatagramPacket(receiveData,
receiveData.length);
clientSocket.receive(receivePacket);
//将回复的包中的数据拿出来
String modifiedSentence =
new String(receivePacket.getData());
System.out.println("FROM SERVER:" +
modifiedSentence);
//关闭客户端,释放资源
clientSocket.close();
}
}
服务器
import java.io.*;
import java.net.*;
public class UDPServer {
public static void main(String args[]) throws Exception
{
DatagramSocket serverSocket = new
DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
while(true)
{
//接受客户端发来的请求
DatagramPacket receivePacket =
new DatagramPacket(receiveData,
receiveData.length);
serverSocket.receive(receivePacket);
//将客户端的请求的信息转为字符数组
String sentence = new String(
receivePacket.getData());
//从请求里拿到客户端IP地址
InetAddress IPAddress =
receivePacket.getAddress();
//拿到客户端端口
int port = receivePacket.getPort();
String capitalizedSentence =
sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
//将要返回的响应打包,里面需要客户端的IP地址,客户端端口
DatagramPacket sendPacket =
new DatagramPacket(sendData,
sendData.length, IPAddress, port);
//服务器发送响应包
serverSocket.send(sendPacket);
}
}
}
到了这里,关于计网实验第二章:TCP与UDP实验的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!