socket TCP/UDP通信实验
TCP通信
TCP通信服务流程
![Alt text](./pic1.PNG)
TCP客户端代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <linux/in.h>
#include <memory.h>
#define PORT 8888
int main() {
int sock_service;
struct sockaddr_in addr_service;
sock_service = socket(AF_INET, SOCK_STREAM, 0);
if (sock_service == -1) {
perror("socked error\n");
return 0;
}
addr_service.sin_family = AF_INET;
addr_service.sin_port = htons(PORT);
addr_service.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(addr_service.sin_zero), 8);
inet_pton(AF_INET, "127.0.0.1", &addr_service.sin_addr);
connect(sock_service, (struct sockaddr*) &addr_service, sizeof(struct sockaddr));
char buffer[1024];
int size=0;
while(1){
size=read(0,buffer,1024);
if(size>0){
write(sock_service,buffer,size);
size=read(sock_service,buffer,1024);
write(1,buffer,size);
}
}
close(sock_service);
}
TCP服务器代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <linux/in.h>
#include <memory.h>
#define PORT 8888
int main() {
int sock_service, sock_client;
struct sockaddr_in addr_service, addr_client;
sock_service = socket(AF_INET, SOCK_STREAM, 0);
if (sock_service == -1) {
perror("socked error\n");
return 0;
}
addr_service.sin_family = AF_INET;
addr_service.sin_port = htons(PORT);
addr_service.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(addr_service.sin_zero), 8);
if (bind(sock_service, (struct sockaddr *) &addr_service, sizeof(struct sockaddr)) == -1) {
perror("bind error\n");
return 0;
}
if (listen(sock_service, 5) == -1) {
perror("listen error\n");
return 0;
}
int addr_length = sizeof(struct sockaddr_in);
sock_client = accept(sock_service, &addr_client, &addr_length);
while (1) {
if (sock_client == -1) {
sock_client = accept(sock_service, &addr_client, &addr_length);
continue;
} else {
char buffer[1024];
int size = read(sock_client, buffer, 1024);
if(size>0) {
printf("message:");
write(1, buffer, size);
sprintf(buffer, "recieved %d bytes\n", size);
write(sock_client, buffer, strlen(buffer));
}
}
}
close(sock_client);
}
UDP通信
UDP通信服务流程
![Alt text](./pic2.PNG)
UDP客户服务代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <linux/in.h>
#include <memory.h>
#define PORT 8888
int main() {
int s;
struct sockaddr_in addr_service,addr_client;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1) {
perror("socked error\n");
return 0;
}
addr_service.sin_family = AF_INET;
addr_service.sin_port = htons(PORT);
addr_service.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(addr_service.sin_zero), 8);
char buffer[128];
int size=0;
int length= sizeof(struct sockaddr);
while(1){
size=read(0,buffer,128);
if(size>0){
sendto(s,buffer,128,0,(struct sockaddr*)&addr_service,sizeof(struct sockaddr));
int n=recvfrom(s,buffer,128,0,(struct sockaddr*)&addr_client,&length);
printf("client received:%s",buffer);
}
}
close(s);
}
UDP服务器代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <linux/in.h>
#include <memory.h>
#define PORT 8888
int main() {
int sock_service;
struct sockaddr_in addr_service, addr_client;
sock_service = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_service == -1) {
perror("socked error\n");
return 0;
}
addr_service.sin_family = AF_INET;
addr_service.sin_port = htons(PORT);
addr_service.sin_addr.s_addr = htonl(INADDR_ANY);
bzero(&(addr_service.sin_zero), 8);
if (bind(sock_service, (struct sockaddr *) &addr_service, sizeof(struct sockaddr)) == -1) {
perror("bind error\n");
return 0;
}
int length= sizeof(struct sockaddr);
while (1){
char buffer[128];
int n=recvfrom(sock_service,buffer,128,0,(struct sockaddr*)&addr_client,&length);
printf("service received:%s",buffer);
sendto(sock_service,buffer,n,0,(struct sockaddr*)&addr_client,sizeof(struct sockaddr));
}
close(sock_service);
}