TCP练习2: 客户端给服务端发送文本,服务端将文本转成大写再返回给客户端

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


import org.junit.Test;


/*
 * 客户端给服务端发送文本,服务端将文本转成大写再返回给客户端
 */
public class TCPExer2 {
/*
* 客户端
*/
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
InputStream is = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
os = socket.getOutputStream();
os.write("abcdefght".getBytes());
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b = new byte[1024];
int len;
while((len = is.read(b)) != -1){
String str = new String(b,0,len);
System.out.println("我是客户端,接收到来自服务端的返回数据:" + str);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

/*
* 服务端
*/
@Test
public void server(){
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
try {
ss = new ServerSocket(9999);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[1024];
int len;
String str = null;
while((len = is.read(b)) != -1){
str = new String(b,0,len);
System.out.println("我是服务端,收到客户端的信息:" + str);
str = str.toUpperCase();
}
s.shutdownInput();
os = s.getOutputStream();
os.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}
下面是客户端的Python程序: ```python import socket # 创建套接字对象 client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 设置服务端的IP和端口号 server_ip = "127.0.0.1" server_port = 8888 # 连接服务端 client_socket.connect((server_ip, server_port)) # 发送消息给服务端 message = "hello world" client_socket.send(message.encode()) # 接收服务端发送回来的消息,并将收到的小写字符串转为大写 recv_message = client_socket.recv(1024).decode().upper() # 打印收到的消息 print("Received message:", recv_message) # 关闭套接字 client_socket.close() ``` 下面是服务端的Python程序: ```python import socket # 创建套接字对象 server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 设置服务端的IP和端口号 server_ip = "127.0.0.1" server_port = 8888 # 绑定套接字对象到本地IP和端口号 server_socket.bind((server_ip, server_port)) # 监听来自客户端的连接 server_socket.listen(1) # 循环等待客户端连接 while True: # 接受客户端连接请求,返回客户端套接字对象和客户端地址信息 client_socket, client_addr = server_socket.accept() # 接收客户端发送的消息 recv_message = client_socket.recv(1024).decode() # 将接收到的小写字符串转为大写,并发送客户端 upper_message = recv_message.upper() client_socket.send(upper_message.encode()) # 关闭客户端套接字 client_socket.close() ``` 这两个Python程序分别表示了客户端服务端的过程,通过socket模块实现了TCP传输协议,客户端字符串发送服务端服务端接收并转为大写发送客户端客户端接收并打印接收到的消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值