본문 바로가기
임베디드/C언어 소켓 통신

Sever socket created Sever socket binding Sever socket listening

by sj0020 2020. 9. 23.

hello_server.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define SERVER_IP       INADDR_ANY
#define SERVER_PORT     "9988"

void error_handling(char *message);

int main(int argc, char *argv[])
{
	int serv_sock;
	int clnt_sock;

	struct sockaddr_in serv_addr;
	struct sockaddr_in clnt_addr;
	socklen_t clnt_addr_size;

	char message[]="Hello World!";
	
	/*
	if(argc!=2){
		printf("Usage : %s <port>\n", argv[0]);
		exit(1);
	}
	*/
	serv_sock=socket(PF_INET, SOCK_STREAM, 0);
	if(serv_sock == -1)
		error_handling("socket() error");
	printf("Sever socket created\n");
	
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_addr.s_addr=htonl(SERVER_IP);
	serv_addr.sin_port=htons(atoi(SERVER_PORT));
	
	if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr))==-1 )
		error_handling("bind() error"); 
	printf("Sever socket binding\n");
	
	if(listen(serv_sock, 5)==-1)
		error_handling("listen() error");
	printf("Sever socket listening\n");
	
	clnt_addr_size=sizeof(clnt_addr);  
	clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_addr,&clnt_addr_size);
	if(clnt_sock==-1)
		error_handling("accept() error");  
	printf("Sever socket accepted\n");
	
	write(clnt_sock, message, sizeof(message));
	printf("Sever --> client data sent\n");
	close(clnt_sock);
	printf("client socket closed\n");
	close(serv_sock);
	printf("Sever socket closed\n");
	return 0;
}

void error_handling(char *message)
{
	fputs(message, stderr);
	fputc('\n', stderr);
	exit(1);
}

 

hello_client.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define SERVER_IP	"127.0.0.1"
#define SERVER_PORT "9988"

void error_handling(char *message);

int main(int argc, char* argv[])
{
	int sock;
	struct sockaddr_in serv_addr;
	char message[30] = {0,}; //초기화 하는것이 더 좋다
	int str_len = 0; //초기화 하는것이 더 좋다
	
	/*
	if(argc!=3){
		printf("Usage : %s <IP> <port>\n", argv[0]);
		exit(1);
	}
	*/
	
	sock=socket(PF_INET, SOCK_STREAM, 0);
	if(sock == -1)
		error_handling("socket() error");
	printf("client socket created\n");
	
	memset(&serv_addr, 0, sizeof(serv_addr));
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_addr.s_addr=inet_addr(SERVER_IP);
	serv_addr.sin_port=htons(atoi(SERVER_PORT));
		
	if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1) 
		error_handling("connect() error!");
	printf("client socket connected\n");
	
	str_len=read(sock, message, sizeof(message)-1);
	if(str_len==-1)
		error_handling("read() error!");
	printf("server -> client data read\n");
	printf("Message from server: %s \n", message);  
	close(sock);
	printf("client socket closed\n");
	return 0;
}

void error_handling(char *message)
{
	fputs(message, stderr);
	fputc('\n', stderr);
	exit(1);
}

'임베디드 > C언어 소켓 통신' 카테고리의 다른 글

echo_server.c echo_client.c  (0) 2020.09.24
hello server (client ->server)  (0) 2020.09.24
hello server (client ->server)  (0) 2020.09.23
c언어 소켓통신 hello_client.c  (0) 2020.09.23
C언어 소켓통신 hello_server.c  (0) 2020.09.23