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

hello server (client ->server)

by sj0020 2020. 9. 24.

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 serv_sock;
	int clnt_sock;

	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);
	}
	*/
	char message[30] = "Hello SERVER!";
	
	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");
	
	write(sock, message, sizeof(message));
	printf("client --> Server data sent\n");
	close(serv_sock);
	printf("server socket closed\n");
	close(clnt_sock);
	printf("client socket closed\n");
	return 0;

	/*
	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);
}

 

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;

	int sock;

	struct sockaddr_in serv_addr;
	struct sockaddr_in clnt_addr;
	socklen_t clnt_addr_size;

	char message[30] = { 0, }; //초기화 하는것이 더 좋다
	int str_len = 0; //초기화 하는것이 더 좋다

	//char message[] = "Hello SERVER!";
	
	/*
	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;
	*/

	str_len = read(clnt_sock, message, sizeof(message) - 1);
	if (str_len == -1)
		error_handling("read() error!");
	printf("client -> Server data read\n");
	printf("Message from client: %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);
}