'Education/Networking'에 해당되는 글 2건

  1. 2009.08.27 TCP by 초상큼발랄 1
  2. 2009.08.27 UDP by 초상큼발랄

TCP

Education/Networking 2009. 8. 27. 18:19


<server.c>

#include <stdio.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <signal.h>

#define SERV_PORT 62000
#define RECV_BUF_LEN 80

void sig_alrm(int signo);

void sig_alrm(int signo)
{

 printf("alarm...\n");

}

void sig_child(int signo)
{

 printf("Client terminated \n");

}


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

 int sfd, cfd;
 struct sockaddr_in saddr;
 struct sockaddr_in caddr;
 int size, len;
 char recv_buf[RECV_BUF_LEN];

 pid_t pid;

 sfd = socket(AF_INET, SOCK_STREAM, 0);
 len = sizeof(saddr);

 bzero((char*)&saddr, sizeof(saddr));
 saddr.sin_family = AF_INET;
 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
 saddr.sin_port = htons(SERV_PORT);

 if ( bind(sfd, (struct sockaddr*)&saddr, sizeof(saddr)) < 0 ) {

  printf("Bind Error\n");
  return -1;

 }

 listen(sfd, 5);

 signal(SIGALRM, sig_alrm); 
 signal(SIGCHLD, sig_child); 
 //signal(SIGINT, SIG_IGN); //ignore 
 size = sizeof(caddr);

 while (1)
 {

  cfd = accept(sfd, (struct sockaddr*)&caddr, &size);//connect
  printf("Client : %d\n", cfd);

  pid = fork(); // call pre-fork() function

  if(pid <0)
  {
   printf("Fork Error!!\n");
   exit(-1);

  }

  else if(pid==0) //child process
  {

  // alarm(3);
   while(len = read(cfd, recv_buf, RECV_BUF_LEN))
   {

    recv_buf[len] = '\0';
    printf("recv : %s", recv_buf);
    

    if(strncmp(recv_buf, "exit",4) == 0)//client request termination.
    { 

//     close(cfd);
//     close(sfd);
//     return 0;
     exit(1);

    }         // alarm(0);

  else//parent process
  {
  }

 }

}

}

 close(cfd);
 close(sfd);

 return 0;

}






<client.c>

#include <stdio.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define SERV_PORT 62000
#define RECV_BUF_LEN 1024
#define MSG_SIZE 1024

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

 int sfd;
 struct sockaddr_in saddr;
 int len, msglen;
 static char msg[MSG_SIZE];

 sfd = socket(AF_INET, SOCK_STREAM, 0);

 bzero((char*)&saddr, sizeof(saddr));
 saddr.sin_family = AF_INET;
 saddr.sin_addr.s_addr = inet_addr(argv[1]);
 saddr.sin_port = htons(SERV_PORT);

 if (connect(sfd, (struct sockaddr*)&saddr, sizeof(saddr)) < 0) {

  printf("connect error\n");
  return -1;

 }

 while (1)
{

  msglen = read(STDIN_FILENO, msg, MSG_SIZE);

  if (len = write(sfd, msg, msglen) < msglen)
{

   printf("write error\n");

  }

  if (strncmp(msg, "exit", 4) == 0)
  {

   close(sfd);
   return 0;

  }

 }

// close(sfd);

 return 0;

}


 

'Education > Networking' 카테고리의 다른 글

UDP  (0) 2009.08.27
Posted by 초상큼발랄
l

UDP

Education/Networking 2009. 8. 27. 18:09


<server_udp.c>

#include <stdio.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <signal.h>


void error(char *msg)
{

 perror(msg);
 exit(0);

}

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

 int sock, length, fromlen, n;
 struct sockaddr_in server;
 struct sockaddr_in from;

 char buf[1024];

 if(argc < 2)
 {

  fprintf(stderr, "ERROR, no port provided\n");
  exit(0);

 }

 sock = socket(AF_INET, SOCK_DGRAM, 0);

 if(sock <0) error("Opening socket");

 length = sizeof(server);

 bzero(&server, length);
 server.sin_family = AF_INET;
 server.sin_addr.s_addr  = INADDR_ANY;
 //server.sin_port  = htons(atoi(argv[1]));
 server.sin_port  = htons(8024);
 
 if( bind(sock, (struct sockaddr *)&server, length) < 0 )
  error("Binding");
 fromlen = sizeof(struct sockaddr_in);

 while (1)
 {

  n = recvfrom(sock, buf, 256, 0, (struct sockaddr *)&from, &fromlen);

  if(n <0)
  { 

   error("recvfrom");
   printf("Receive size 0\n");
   exit(-1);

  }
  
  write( 1, "Received a datagram: ", 21);
  buf[n] = '\0';
  write( 1, buf, n);

  n = sendto(sock,"Got your msg\n",13, 0, (struct sockaddr *)&from, sizeof(from));

  if(n <0) error("sendto");

 }

}



<client_udp.c>

#include <stdio.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#include <signal.h>

void error(char *);

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

 int sock, length, n;
 struct sockaddr_in server, from;
 struct hostent *hp;

 char buffer[256];
 
 if(argc != 3)
 {

  printf("Usage: server port \n");
  exit(1);

 }

 sock = (AF_INET, SOCK_DGRAM, 0);
 if(sock <0) error("Socket\n");

 server.sin_family = AF_INET;
 hp = gethostbyname(argv[1]);
 if(hp==0) error("Unknown host\n");

 bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
 //server.sin_port = htons(atoi(argv[2]));
 server.sin_port = htons(8024);
 length = sizeof(struct sockaddr_in);
 printf("Please enter the message");

 bzero(buffer, 256);
 fgets(buffer, 255, stdin);

 n = sendto(sock, buffer, strlen(buffer), 0,(struct sockaddr *)&server, sizeof(server));
 if(n < 0) error("Sendto\n");

 n = recvfrom(sock, buffer, 256, 0, (struct sockaddr *)&server, &length);
 if(n < 0) error("recvfrom\n");

 buffer[n] = '\0';

 write(1, "Got an ack: ",12);
 write(1, buffer,n);

}

void error(char *msg)

 perror(msg);
 exit(0);

}

'Education > Networking' 카테고리의 다른 글

TCP  (1) 2009.08.27
Posted by 초상큼발랄
l