본문 바로가기

[+] Information/[-] Network

[2009/06/01] 예제소스(helloworld_server2.c, echo_server_win.c, echo_client_win.c, echo_server.c, echo_client.c)

/* helloworld_server2.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>

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;
  int clnt_addr_size;

  char message[]="Hello World!\n";

  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");

  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");
   

  if( listen(serv_sock, 5)==-1 )
    error_handling("listen() error");
 

  /* 대기 큐에 존재하는 클라이언트 연결요청 계속해서 수락 */
  while(1){   
 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");
      break;
    }

    /* 데이터 전송후 종결 */
    write(clnt_sock, message, sizeof(message));
    close(clnt_sock);
  }

  return 0;
}


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





/* echo_server_win.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>

#define BUFSIZE 1024

void ErrorHandling(char *message);

int main(int argc, char **argv)
{
  WSADATA wsaData;
  SOCKET hServSock;
  SOCKET hClntSock;
  char message[BUFSIZE];
  int strLen;

  SOCKADDR_IN servAddr;
  SOCKADDR_IN clntAddr;
  int clntAddrSize;

  if(argc!=2){
    printf("Usage : %s <port>\n", argv[0]);
    exit(1);
  }

  if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) /* Load Winsock 2.2 DLL */
  ErrorHandling("WSAStartup() error!");

  hServSock=socket(PF_INET, SOCK_STREAM, 0);  
  if(hServSock == INVALID_SOCKET)
    ErrorHandling("socket() error");
 
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family=AF_INET;
  servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
  servAddr.sin_port=htons(atoi(argv[1]));

  if(bind(hServSock, (SOCKADDR*) &servAddr, sizeof(servAddr))==SOCKET_ERROR)
    ErrorHandling("bind() error");

  if(listen(hServSock, 5)==SOCKET_ERROR)
    ErrorHandling("listen() error");

  clntAddrSize=sizeof(clntAddr);   
  hClntSock=accept(hServSock, (SOCKADDR*)&clntAddr,&clntAddrSize);
  if(hClntSock==INVALID_SOCKET)
  ErrorHandling("accept() error");
  
  while( (strLen=recv(hClntSock, message, BUFSIZE, 0)) != 0){ /* 데이터 수신 및 전송 */
    send(hClntSock, message, strLen, 0);
  }
  closesocket(hClntSock);
  WSACleanup();
  return 0;
}

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






/* echo_client_win.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>

#define BUFSIZE 1024
void ErrorHandling(char *message);

int main(int argc, char **argv)
{
  WSADATA wsaData;
  SOCKET hSocket;
  char message[BUFSIZE];
  int strLen;

  SOCKADDR_IN servAddr;

  if(argc!=3){
    printf("Usage : %s <IP> <port>\n", argv[0]);
    exit(1);
  }

  if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) /* Load Winsock 2.2 DLL */
  ErrorHandling("WSAStartup() error!");

  hSocket=socket(PF_INET, SOCK_STREAM, 0);  
  if(hSocket == INVALID_SOCKET)
    ErrorHandling("socket() error");

  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin_family=AF_INET;
  servAddr.sin_addr.s_addr=inet_addr(argv[1]);
  servAddr.sin_port=htons(atoi(argv[2]));

  if(connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr))==SOCKET_ERROR)
    ErrorHandling("connect() error!");

  while(1) {
     fputs("전송할 메시지를 입력 하세요 (q to quit) : ", stdout);
     fgets(message, BUFSIZE, stdin); /* 전송 할 데이터 콘솔로부터 입력 */
     
     if(!strcmp(message,"q\n"))
  break;
     send(hSocket, message, strlen(message), 0); /* 메시지 전송 */
   
     strLen=recv(hSocket, message, BUFSIZE-1, 0); /* 메시지 수신 */
     message[strLen]=0;
     printf("서버로부터 전송된 메시지 : %s \n", message);
  }

  closesocket(hSocket);
  WSACleanup();
  return 0;
}

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






/* echo_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 1024

void error_handling(char *message);

int main(int argc, char **argv)
{
 int serv_sock;
 int clnt_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_STREAM, 0);  
 if(serv_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=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");
 
 if(listen(serv_sock, 5)==-1)
  error_handling("listen() error");
 
 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");
 
 /* 데이터 수신 및 전송 */
 while( (str_len=read(clnt_sock,message, BUFSIZE)) != 0){
  write(clnt_sock, message, str_len);
  write(1, message, str_len);
 }
 close(clnt_sock);
 return 0;
}

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







/* echo_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 1024

void error_handling(char *message);

int main(int argc, char **argv)
{
 int sock;
 char message[BUFSIZE];
 int str_len;
 struct sockaddr_in serv_addr;

 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");
 
 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]));
 
 if(connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1)
  error_handling("connect() error!");
 
 while(1) {
  /* 메세지 입력, 전송 */
  fputs("전송할 메시지를 입력 하세요 (q to quit) : ", stdout);
  fgets(message, BUFSIZE, stdin);
  
  if(!strcmp(message,"q\n")) break;
  write(sock, message, strlen(message));
  
  /* 메세지 수신, 출력 */
  str_len=read(sock, message, BUFSIZE-1);
  message[str_len]=0;
  printf("서버로부터 전송된 메시지 : %s \n", message);
 }
 
 close(sock);
 return 0;
}

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