套接字编程实验报告 本文关键词:编程,实验,报告
套接字编程实验报告 本文简介:Programing实验物联2班201208080228赵智慧一、实验题目:利用套接字编程,编写出TCP和UDP的客户机、服务器程序进行交互二、实验代码:1.TCP服务器:packageTCP;importjava.io.BufferedReader;importjava.io.IOExceptio
套接字编程实验报告 本文内容:
Programing
实验
物联2班
201208080228
赵智慧
一、
实验题目:利用套接字编程,编写出TCP和UDP的客户机、服务器程序进行交互
二、
实验代码:
1.
TCP服务器:
package
TCP;
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.net.ServerSocket;
import
java.net.Socket;
public
class
TCPServer
{
public
static
void
main(String[]
args)
throws
IOException{
ServerSocket
server=new
ServerSocket(7999);
System.out.println(“server
ok“);
Socket
socket=server.accept();
BufferedReader
bf=new
BufferedReader(new
InputStreamReader(socket.getInputStream()));
String
str=bf.readLine();
while(str!=null)
{
System.out.println(str);
str=bf.readLine();
}
}
}
2.
TCP客户机:
package
TCP;
import
java.io.BufferedReader;
import
java.io.DataOutputStream;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.io.OutputStream;
import
java.io.PrintWriter;
import
java.net.Socket;
import
java.net.UnknownHostException;
public
class
TCPClient
{
public
static
void
main(String[]
args)
throws
UnknownHostException,IOException{
String
sentence;
InputStreamReader
isr
=
new
InputStreamReader(System.in);
BufferedReader
inFromUser
=
new
BufferedReader(isr);
Socket
clientSocket
=
new
Socket(“127.0.0.1“,7999);
sentence
=
inFromUser.readLine();
OutputStream
os
=
clientSocket.getOutputStream();
PrintWriter
oos=new
PrintWriter(os);
os.write(sentence.getBytes());
clientSocket.close();
}
}
3.
UDP服务器:
package
UDP;
import
java.net.DatagramPacket;
import
java.net.DatagramSocket;
import
java.net.InetAddress;
import
java.net.SocketException;
public
class
UDPServer
{
public
static
void
main(String[]
args)
throws
Exception{
DatagramSocket
serverSocket
=
new
DatagramSocket(9876);
//所有发送和接收的数据都将通过该套接字
//因为UDP无连接,所以无需创建一个新的套接字监听新的请求
byte[]
receive1
=
new
byte[1024];
byte[]
receive2
=
new
byte[1024];
byte[]
send1
=
new
byte[1024];
byte[]
send2
=
new
byte[1024];
byte[]
send3
=
new
byte[1024];
while(true){
DatagramPacket
receivePacket
=
new
DatagramPacket(receive1,receive1.length);
serverSocket.receive(receivePacket);
String
str
=
new
String(receivePacket.getData());
//从分组中提取出数据,并存入str中
InetAddress
ip
=
receivePacket.getAddress();
//提取IP地址
int
port
=
receivePacket.getPort();
//提取客户机端口号
String
ask
=
“请选择:1.将其转化为大写
2.将a字符替换为c字符“;
send1
=
ask.getBytes();
DatagramPacket
sendPacket1
=
new
DatagramPacket(send1,send1.length,ip,port);
serverSocket.send(sendPacket1);
DatagramPacket
receivePacket2
=
new
DatagramPacket(receive2,receive2.length);
serverSocket.receive(receivePacket2);
String
str2
=
new
String(receivePacket2.getData());
if(str2.toCharArray()[0]==
1
){
String
capitalizedSentence
=
str.toUpperCase();
send3
=
capitalizedSentence.getBytes();
DatagramPacket
sendPacket3
=
new
DatagramPacket(send3,send3.length,ip,port);
serverSocket.send(sendPacket3);
}
else{
String
change
=
str.replace(
a,c
);
send2
=
change.getBytes();
DatagramPacket
sendPacket2
=
new
DatagramPacket(send2,send2.length,ip,port);
serverSocket.send(sendPacket2);
}
}
}
}
4.
UDP客户机:
package
UDP;
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.net.DatagramPacket;
import
java.net.DatagramSocket;
import
java.net.InetAddress;
import
java.net.SocketException;
import
java.net.UnknownHostException;
public
class
UDPClient
{
public
static
void
main(String[]
args)
throws
IOException{
InputStreamReader
isr
=
new
InputStreamReader(System.in);
BufferedReader
inFromUser
=
new
BufferedReader(isr);
DatagramSocket
clientSocket
=
new
DatagramSocket();
//客户机套接字
InetAddress
IPAddress
=
InetAddress.getByName(“127.0.0.1“);
byte[]
sendData1
=
new
byte[1024];
byte[]
sendData2
=
new
byte[1024];
byte[]
receiveData1
=
new
byte[1024];
byte[]
receiveData2
=
new
byte[1024];
String
sentence
=
inFromUser.readLine();
sendData1
=
sentence.getBytes();
DatagramPacket
sendPacket
=
new
DatagramPacket(sendData1,sendData1.length,IPAddress,9876);
clientSocket.send(sendPacket);
DatagramPacket
receivePacket1
=
new
DatagramPacket(receiveData1,receiveData1.length);
clientSocket.receive(receivePacket1);
String
modifiedData1
=
new
String(receivePacket1.getData());
System.out.println(“Server:“+modifiedData1);
String
sentence2
=
inFromUser.readLine();
sendData2
=
sentence2.getBytes();
DatagramPacket
sendPacket2
=
new
DatagramPacket(sendData2,sendData2.length,IPAddress,9876);
clientSocket.send(sendPacket2);
DatagramPacket
receivePacket2
=
new
DatagramPacket(receiveData2,receiveData2.length);
clientSocket.receive(receivePacket2);
String
modifiedData2
=
new
String(receivePacket2.getData());
System.out.println(“Server:“+modifiedData2);
clientSocket.close();
}
}
三、
实验分析:
TCP提供可靠的数据传输,而UDP提供不可靠的运输服务,在套接字编程方面,UDP客户机使用的是DatagramSocket,而TCP客户机使用的是Socket。
TCP实现:
(1)
客户机从键盘输入读取一行字符,并通过套接字将该行发送到服务器
(2)
服务器从其套接字读取一行数据
(3)
服务器将来自客户机的数据打印出来
UDP实现:
(1)
客户机从其键盘输入读取一行字符,并通过套接字将该行发送到服务器
(2)
服务器从其套接字读取一行数据
(3)
服务器发送可供选择的操作信息发送给客户机
(4)
客户机从其套接字读取来自服务器的信息,并将其选择通过套接字发送给服务器
(5)
服务器通过套接字读取到客户机的选择,并根据是选择转换大小写还是选择替换字符来将修改后的行通过套接字发送给客户机
(6)
客户机从其套接字中读取按照自己选择修改后的行,并打印显示出来
四、
运行结果:
TCP套接字:
运行TCPServer
之后:
再运行TCPClient:
则TCPServer会显示:
UDP套接字:
五、问题思考:
1.
在一台主机上安装编译用Java写的TCPClient和UDPClient程序,在另一台主机上安装编译TCPServer和UDPServer程序
a.
如果你在运行TCPServer之前运行TCPClient,将发生什么现象,为什么?
会报错,因为TCP是面向连接的传输,所以未开启服务器时先开启客户机不能通过三次握手建立连接,也就无法传输数据。
b.
如果你在运行UDPServer之前运行UDPClient,将发生什么现象,为什么?
没什么变化,因为UDP是无连接的,先开启服务器还是客户机都没有影响。
c.
如果对客户端和服务器使用了不同的端口,将发生什么现象?
不影响数据传输。
2.假定在UDPClient.java中,我们使用
DatagramSocket
clientSocket
=
new
DatagramSocket(
5432
);
代替
DatagramSocket
clientSocket
=
new
DatagramSocket();
在UDPServer.java中是否有必要进行修改?UDPClient和UDPServer中的套接字端口号是多少?在变化之前是多少?
没必要修改,UDPClient的
套接字端口是系统随机分配的端口号,而服务器端口号则是程序中设定的,不会发生变化
六、实验心得:
通过这个实验对Socket套接字更为了解了,也更清楚了UDP和TCP之间的差别所在,在UDP客户机和服务器交互过程中我特意设置了一个稍微复杂的交互过程,旨在更为了解数据通过套接字的传输,根据客户机的选择,服务器来提供不同的响应。实验还是学到了很多,也可以顺便巩固Java语言输入输出流的知识。