用c语言,建立一个UDP,TCP的client和serve的socket,并进行通信
时间: 2023-05-28 15:05:36 浏览: 140
UDP:
Client端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8888
#define MAXLINE 1024
int main(int argc, char *argv[]) {
int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket error");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
printf("inet_pton error for %s\n", argv[1]);
exit(1);
}
while (fgets(sendline, MAXLINE, stdin) != NULL) {
sendto(sockfd, sendline, strlen(sendline), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
int n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = 0;
printf("recvline: %s", recvline);
}
close(sockfd);
exit(0);
}
```
Server端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8888
#define MAXLINE 1024
int main(int argc, char *argv[]) {
int sockfd;
struct sockaddr_in servaddr, cliaddr;
char buf[MAXLINE];
socklen_t len;
int n;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket error");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("bind error");
exit(1);
}
while (1) {
len = sizeof(cliaddr);
n = recvfrom(sockfd, buf, MAXLINE, 0, (struct sockaddr *)&cliaddr, &len);
sendto(sockfd, buf, n, 0, (struct sockaddr *)&cliaddr, len);
buf[n] = 0;
printf("recvline: %s", buf);
}
close(sockfd);
exit(0);
}
```
TCP:
Client端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8888
#define MAXLINE 1024
int main(int argc, char *argv[]) {
int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE];
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket error");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
printf("inet_pton error for %s\n", argv[1]);
exit(1);
}
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("connect error");
exit(1);
}
while (fgets(sendline, MAXLINE, stdin) != NULL) {
write(sockfd, sendline, strlen(sendline));
int n = read(sockfd, recvline, MAXLINE);
recvline[n] = 0;
printf("recvline: %s", recvline);
}
close(sockfd);
exit(0);
}
```
Server端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8888
#define MAXLINE 1024
int main(int argc, char *argv[]) {
int listenfd, connfd;
struct sockaddr_in servaddr, cliaddr;
char buf[MAXLINE];
socklen_t len;
int n;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket error");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("bind error");
exit(1);
}
if (listen(listenfd, 10) < 0) {
perror("listen error");
exit(1);
}
while (1) {
len = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &len);
while ((n = read(connfd, buf, MAXLINE)) > 0) {
write(connfd, buf, n);
buf[n] = 0;
printf("recvline: %s", buf);
}
close(connfd);
}
close(listenfd);
exit(0);
}
```
阅读全文