#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define PORT 8887
#define BUFFER_SIZE 1024
int main()
{ int sock;
sock=socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in sockaddrin;
memset(&sockaddrin,0, sizeof(sockaddrin));
sockaddrin.sin_family=AF_INET;
sockaddrin.sin_port=htons(PORT);
sockaddrin.sin_addr.s_addr=inet_addr("127.0.0.1");
if(connect(sock,(struct sockaddr *)&sockaddrin, sizeof(sockaddrin))<0)
{
perror("error");
exit(1);
}
char sendbuf[BUFFER_SIZE];
char recvbuf[BUFFER_SIZE];
while(fgets(sendbuf, sizeof(sendbuf),stdin)!= NULL)
{
send(sock,sendbuf, strlen(sendbuf),0);
if (strcmp(sendbuf, "exit\n") == 0)
break;
recv(sock,recvbuf, sizeof(recvbuf),0);
fputs(recvbuf,stdout);
memset(sendbuf,0,sizeof(sendbuf));
memset(recvbuf,0,sizeof(recvbuf));
}
close(sock);
return 0;
}
linux socket
最新推荐文章于 2024-02-07 10:34:44 发布