본문 바로가기

[+] Information/[-] Network

[2009/06/09] 예제소스(uecho_server.c, uecho_client.c, becho_server.c, becho_client.c)

/* uecho_server.c */


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

#define BUFSIZE 30

void error_handling(char *message);

int main(int argc, char **argv)
{
 int serv_sock;
 char message[BUFSIZE];
 int str_len;
 
 struct sockaddr_in serv_addr;
 struct sockaddr_in clnt_addr;
 int clnt_addr_size;
 
 if(argc!=2){
  printf("Usage : %s <port>\n", argv[0]);
  exit(1);
 }
 
 serv_sock=socket(PF_INET, SOCK_DGRAM, 0);
 if(serv_sock == -1)
  error_handling("UDP 소켓 생성 오류");
 
 memset(&serv_addr, 0, sizeof(serv_addr));
 serv_addr.sin_family=AF_INET;
 serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
 serv_addr.sin_port=htons(atoi(argv[1]));
 
 if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr))==-1)
  error_handling("bind() error");
 
 while(1) {
  clnt_addr_size=sizeof(clnt_addr);
  str_len = recvfrom(serv_sock, message, BUFSIZE, 0, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
  sendto(serv_sock, message, str_len, 0, (struct sockaddr*)&clnt_addr, sizeof(clnt_addr));
 }
 
 close(serv_sock);
 return 0;
}

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




/* uecho_client.c */

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

#define BUFSIZE 30

void error_handling(char *message);

int main(int argc, char **argv)
{
 int sock;
 char message[BUFSIZE];
 int str_len, addr_size;
 
 struct sockaddr_in serv_addr;
 struct sockaddr_in from_addr;
 if(argc!=3){
  printf("Usage : %s <IP> <port>\n", argv[0]);
  exit(1);
 }
 
 sock=socket(PF_INET, SOCK_DGRAM, 0);  
 if(sock == -1)
  error_handling("socket() error");
 
 memset(&serv_addr, 0, sizeof(serv_addr));
 serv_addr.sin_family=AF_INET;
 serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
 serv_addr.sin_port=htons(atoi(argv[2]));
 
 while(1)
 {
  fputs("전송할 메시지를 입력 하세요 (q to quit) : ", stdout);
  fgets(message, sizeof(message), stdin);    
  if(!strcmp(message,"q\n")) break;
  
  sendto(sock, message, strlen(message), 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
  addr_size=sizeof(from_addr);
  str_len = recvfrom(sock, message, BUFSIZE, 0, (struct sockaddr*)&from_addr, &addr_size);

  message[str_len]=0;
  printf("서버로부터 전송된 메시지 : %s", message);
 }
 
 close(sock);
 return 0;
}

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




/* becho_server.c */

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

#define BUFSIZE 30

void error_handling(char *message);

int main(int argc, char **argv)
{
 int serv_sock;
 char message[BUFSIZE];
 int str_len, num=0;
 
 struct sockaddr_in serv_addr;
 struct sockaddr_in clnt_addr;
 int clnt_addr_size;
 
 if(argc!=2){
  printf("Usage : %s <port>\n", argv[0]);
  exit(1);
 }
 
 serv_sock=socket(PF_INET, SOCK_DGRAM, 0);
 if(serv_sock == -1)
  error_handling("UDP 소켓 생성 오류");
 
 memset(&serv_addr, 0, sizeof(serv_addr));
 serv_addr.sin_family=AF_INET;
 serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
 serv_addr.sin_port=htons(atoi(argv[1]));
 
 if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr))==-1)
  error_handling("bind() error");
 
 sleep(5);
 while(1) {
  clnt_addr_size=sizeof(clnt_addr);
  sleep(1);
  str_len = recvfrom(serv_sock, message, BUFSIZE, 0, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
  printf("수신 번호  : %d \n", num++);
  sendto(serv_sock, message, str_len, 0, (struct sockaddr*)&clnt_addr, sizeof(clnt_addr));
 }
 
 return 0;
}

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




/* becho_client.c */

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

#define BUFSIZE 30

void error_handling(char *message);

int main(int argc, char **argv)
{
 int sock;
 char message[BUFSIZE];
 int str_len, addr_size, i;
 
 char MSG1[] = "Good ";
 char MSG2[] = "Evening ";
 char MSG3[] = "Everybody!";
 
 struct sockaddr_in serv_addr;
 struct sockaddr_in from_addr;
 
 if(argc!=3){
  printf("Usage : %s <IP> <port>\n", argv[0]);
  exit(1);
 }
 
 sock=socket(PF_INET, SOCK_DGRAM, 0);  
 if(sock == -1)
  error_handling("UDP 소켓 생성 오류");
 
 memset(&serv_addr, 0, sizeof(serv_addr));
 serv_addr.sin_family=AF_INET;
 serv_addr.sin_addr.s_addr=inet_addr(argv[1]);
 serv_addr.sin_port=htons(atoi(argv[2]));
 sendto(sock, MSG1, strlen(MSG1), 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
 sendto(sock, MSG2, strlen(MSG2), 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
 sendto(sock, MSG3, strlen(MSG3), 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
 
 for(i=0; i<3; i++)
 {
  addr_size = sizeof(from_addr);
  str_len = recvfrom(sock, message, BUFSIZE, 0, (struct sockaddr*)&from_addr, &addr_size);    
  message[str_len]=0;
  printf("서버로부터 수신된 %d차 메시지 : %s \n", i, message);
 }
 close(sock);
 return 0;
}

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