目录
- UDP
-
- 程序流程图
-
- 源码
-
- udp_client.c
- udp_server.c
- TCP
-
- 程序流程图
-
- 源码
-
- tcp_client.c
- tcp_server.c
- 结语
UDP
程序流程图
函数及作用
函数名 |
功能 |
socket() |
创建套接字 |
bind() |
绑定 ip + 套接字 |
recvfrom() |
接收消息 |
sendto() |
发送消息 |
源码
udp_client.c
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int sockfd = 0;
struct sockaddr_in dest_addr;
unsigned short port = 8000;
char *server_ip = "172.20.223.151";
if( argc > 1 )
{
server_ip = argv[1];
}
if( argc > 2 )
{
port = atoi(argv[2]);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
perror("socket error!");
exit(-1);
}
bzero(&dest_addr, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(port);
inet_pton(AF_INET, server_ip, &dest_addr.sin_addr);
printf("ready send data to UDP server %s:%d!\n", server_ip, port);
while(1)
{
char send_buf[512] = "";
char recv_buf[512] = "";
fgets(send_buf, sizeof(send_buf), stdin);
send_buf[strlen(send_buf)-1] = '\0';
sendto(sockfd, send_buf, strlen(send_buf), 0, (struct sockaddr*)&dest_addr, sizeof(dest_addr));
recvfrom(sockfd, recv_buf, sizeof(recv_buf), 0, NULL, NULL);
printf("%s\n", recv_buf);
}
close(sockfd);
return 0;
}
udp_server.c
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int sockfd = 0;
int err_log = 0;
struct sockaddr_in my_addr;
unsigned short port = 8000;
if(argc > 1)
{
port = atoi(argv[1]);
}
printf("UDP Server Started!\n");
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sockfd < 0)
{
perror("socket");
exit(-1);
}
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
printf("Binding server to port %d\n", port);
err_log = bind(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr));
if(err_log != 0)
{
perror("bind");
close(sockfd);
exit(-1);
}
printf("waiting data from other client...\n");
while(1)
{
char recv_buf[512] = "";
char cli_ip[INET_ADDRSTRLEN] = "";
int recv_len = 0;
struct sockaddr_in client_addr;
socklen_t cliaddr_len = sizeof(client_addr);
recv_len = recvfrom(sockfd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr*)&client_addr, &cliaddr_len);
printf("client ip = %s\n", inet_ntop(AF_INET, &client_addr.sin_addr, cli_ip, INET_ADDRSTRLEN));
sendto(sockfd, recv_buf, recv_len, 0, (struct sockaddr*)&client_addr, cliaddr_len);
}
close(sockfd);
return 0;
}
TCP
程序流程图
函数及作用
函数名 |
功能 |
socket() |
创建套接字 |
bind() |
套接字绑定端口 + IP |
listen() |
监听端口号是否有连接,等待建立 |
accept() |
调取列表中已建立的连接 |
write() |
写 |
read() |
读 |
close() |
关闭文件标识符 |
accept() |
client 的套接字连接 Server 的套接字 |
源码
tcp_client.c
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
unsigned short port = 8080;
char *server_ip = "192.168.20.61";
char send_buf[512] = "A";
char recv_buf[512] = "";
int sockfd = 0;
int err_log = 0;
struct sockaddr_in server_addr;
if(argc > 1)
{
server_ip = argv[1];
}
if (argc > 2)
{
port = atoi(argv[2]);
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0)
{
perror("socket");
exit(-1);
}
err_log = connect(sockfd,(struct sockaddr*)&server_addr,sizeof(server_addr));
if(err_log != 0)
{
perror("connect");
close(sockfd);
exit(-1);
}
send(sockfd, send_buf, strlen(send_buf), 0);
recv(sockfd, recv_buf, sizeof(recv_buf), 0);
printf("%s\n", recv_buf);
close(sockfd);
return 0;
}
tcp_server.c
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
char recv_buf[2048] = "";
int sockfd = 0;
int connfd = 0;
int err_log = 0;
struct sockaddr_in my_addr;
unsigned short port = 80000;
if(argc > 1)
{
port = atoi(argv[1]);
}
printf("TCP Server Started at port %d\n",port);
sockfd = socket(AF_INET,SOCK_STREAM,0);
if(sockfd < 0)
{
perror("socket");
exit(-1);
}
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(port);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
printf("Binding server to port %d\n", port);
err_log = bind(sockfd, (struct sockaddr*)&my_addr, sizeof(my_addr));
if( err_log != 0)
{
perror("binding");
close(sockfd);
exit(-1);
}
err_log = listen(sockfd, 10);
if(err_log != 0)
{
perror("listen");
close(sockfd);
exit(-1);
}
printf("Waiting client...\n");
while (1)
{
size_t recv_len = 0;
struct sockaddr_in client_addr;
char client_ip[INET_ADDRSTRLEN] = "";
socklen_t cliaddr_len = sizeof(client_addr);
connfd = accept(sockfd,(struct sockaddr*)&client_addr,&cliaddr_len);
if(connfd < 0)
{
perror("accept");
continue;
}
inet_ntop(AF_INET,&client_addr.sin_addr,client_ip,INET_ADDRSTRLEN);
printf("client ip = %s\n",client_ip);
while ((recv_len = recv(connfd,recv_buf,sizeof(recv_buf),0)) > 0)
{
send(connfd,recv_buf,recv_len,0);
}
close(connfd);
printf("client closed!\n");
}
close(sockfd);
return 0;
}
结语
~ ~ ~