Skip to content

Instantly share code, notes, and snippets.

Created January 12, 2023 08:29
What would you like to do?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define IPADDRESS ""
#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");
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment