This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#define IPADDRESS "127.0.0.1" | |
#define SERV_PORT 8080 | |
#define MAXSIZE 1024 | |
int main(int argc, char *argv[]) { | |
int sockfd; | |
struct sockaddr_in servaddr; | |
// socket: create socket | |
sockfd = socket(AF_INET, SOCK_STREAM, 0); | |
memset(&servaddr, 0, sizeof(servaddr)); | |
servaddr.sin_family = AF_INET; | |
servaddr.sin_port = htons(SERV_PORT); | |
inet_pton(AF_INET, IPADDRESS, &servaddr.sin_addr); | |
// connect: initiate a connection on a socket | |
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); | |
while (1) { | |
char buf[MAXSIZE] = {0}; | |
scanf("%s", buf); | |
printf("sent msg:%s\n", buf); | |
send(sockfd, buf, strlen(buf), 0); | |
memset(buf, 0, sizeof(buf)); | |
recv(sockfd, buf, MAXSIZE, 0); | |
printf("read msg:%s\n", buf); | |
if (strcmp(buf, "bye") == 0) { | |
printf("close client!\n"); | |
close(sockfd); | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment