#客户端代码实现
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int mian()
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
assert(sockfd != -1);
struct sockaddr_in saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = thons(6000);
saddr.sin_addr.s_saddr = inet_addr("192.168.1.109");
int res = connect(sockfd, (struct sockaddr*)&saddr, sizeof(saddr));
assert(res != -1);
while (1)
{
char buff[128] = [0];
printf("input:\n");
fgets(buff, 128, stdin);
if (strncmp(buff, "end", 3) == 0)
{
break;
}
send(sockfd, buff, strlen(buff), 0);
memset(buff, 0, 128);
recv(sockfd, buff, 127, 0);
printf("buff =%s\n", buff);
}
close(socfd);
}
纯代码!勿喷!谢谢!