TCP和UDP客户端及服务器端实验报告
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
TCP和UDP客户端及服务器端实验报告
一、原理
1.基于TCP协议的服务器端程序流程:
1)创建套接字(socket)
2)绑定套接字(bind)
3)将套接字设为监听,准备接收客户请求(listen)
4)等待客户请求的到来,当请求到来后,接受请求,返回一个对应于此次连接的套接字(accept)
5)用返回的套接字与客户端进行通信(send/recv)
6)返回,等待另一客户请求
7)关闭套接字
2.基于TCP协议的客户端程序流程
1)创建套接字(socket)
2)向服务器发出连接请求(connect)
3)和服务器端进行通信(send/recv)
4)关闭套接字
在服务器端调用accept函数时,程序就会等待客户端调用connect函数发出连接请求,然后接收请求,于是
双方就建立了连接,之后,服务器端和客户端就可以利用send和recv函数进行通信了。
3.基于UDP的服务器端编写
1)创建套接字(socket)
2)绑定(bind)
3)等待接收数据(recvfrom)
4)关闭套接字
4.基于UDP的客户端编写
1)创建套接字(socket)
2)向服务器发送数据(sendto)
3)关闭套接字
在所有的套接字编程中第一步都是加载套接字库(WSAStartup)
对于每一个WSAStartup函数的成功调用,在最后都要对应一个WSACleanUp调用。
TCP代码实现:首先要建两个工程,不妨设为tcpsrv和tcpclient,分别为客户端和服务器端
tcpsrv.cpp
//TCP server
//listen port 9102
//receive string and display it
//Visual C++ 6.0
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define BUFLEN 1024
int main()
{
SOCKET serversoc;
SOCKET clientsoc;
SOCKADDR_IN serveraddr;
SOCKADDR_IN clientaddr;
char buf[BUFLEN];
int len;
WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa);//initial Ws2_32.dll by a process
if((serversoc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0)//create a tcp socket {
printf("Create socket fail!\n");
return -1;
}
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(9102);
serveraddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
if(bind(serversoc, (SOCKADDR *)&serveraddr, sizeof(serveraddr)) != 0)
{
printf("Bind fail!\n");
return -1;
}
//start listen, maximum length of the queue of pending connections is 1
printf("Start listen...\n");
if(listen(serversoc, 1) != 0)
{
printf("Listen fail!\n");
return -1;
}
len = sizeof(SOCKADDR_IN);
//waiting for connecting
if((clientsoc = accept(serversoc, (SOCKADDR *)&clientaddr, &len))<=0) {
printf("Accept fail!\n");
return -1;
}
printf("Connected\n");
while(1)
{
//waiting for data receive
if(recv(clientsoc, buf, BUFLEN, 0) <= 0)
{
//some error occur
printf("Close connection\n");
closesocket(clientsoc);
break;
}
printf("%s\n",buf);
}
WSACleanup(); //clean up Ws2_32.dll
return 0;
}
相应的客户端程序,tcpclient.cpp
//TCP client
//client send string to server
//Visual C++ 6.0
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main()
{
SOCKET soc;
SOCKADDR_IN serveraddr;
SOCKADDR_IN clientaddr;
unsigned char buf[1024];
WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa);//initial Ws2_32.dll by a process
if((soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <= 0)//create a tcp socket {
printf("Create socket fail!\n");
return -1;
}
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(9102);
serveraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
//connect to server
printf("Try to connect...\n");
if(connect(soc, (SOCKADDR *)&serveraddr, sizeof(serveraddr)) != 0)
{
printf("Connect fail!\n");
return -1;
}
printf("Connected\n");
while(1)
{
scanf("%s", buf);
//send to server
if(send(soc, buf, strlen(buf)+1, 0)<=0)
{
printf("Error!\n");
}
}
WSACleanup(); //clean up Ws2_32.dll
return 0;
}
UDP服务器端:
//UDP server
//listen port 9102
//receive string and display it
//Visual C++ 6.0
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define BUFLEN 1024
int main(void)
{
SOCKET soc;
SOCKADDR_IN addr;
char buf[BUFLEN];
int len;
WSADATA wsa;
WSAStartup(MAKEWORD(1,1),&wsa);//initial Ws2_32.dll by a process memset(&addr, 0, sizeof(addr));
if((soc = socket(AF_INET,SOCK_DGRAM,0)) <= 0)
{
printf("Create socket fail!\n");
return -1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(9102);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(soc,(struct sockaddr *)&addr,sizeof(struct sockaddr))!=0)
{
printf("Bind fail!\n");
return -1;
}
len = sizeof(addr);
printf("start listen...\n");
while(1) {
recvfrom(soc, buf, BUFLEN, 0,(struct sockaddr*)&addr, &len);
printf("%s\n",buf);
}
WSACleanup(); //关闭
return 0;
}
客户端:
//UDP client
//client send string to server
//Visual C++ 6.0
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define BUFLEN 1024
int main(void)
{
SOCKET soc;
SOCKADDR_IN addr;
unsigned char buf[BUFLEN];
WSADATA wsa;
WSAStartup(MAKEWORD(2,2),&wsa);//initial Ws2_32.dll by a process
memset(&addr, 0, sizeof(addr));
if((soc = socket(AF_INET,SOCK_DGRAM,0)) <= 0)
{
printf("Create socket fail!\n");
return -1;
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(9102);
bind(soc,(struct sockaddr *)&addr,sizeof(addr));
while(1) {
scanf("%s", buf);
sendto(soc, buf, strlen(buf)+1, 0, (struct sockaddr *)&addr, sizeof(addr));
}
WSACleanup();//clean up Ws2_32.dll
return 0;
}。