2014년 1월 23일 목요일

LINUX 에코 서버 & 파일 전송 c소스

먼저 클라이언트부분
  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(){
 int file;
 int sockfd;
 int len;
 int buf;
 struct sockaddr_in address;
 int result;
 char put_string[255]={0};
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 address.sin_family = AF_INET;
 address.sin_addr.s_addr = inet_addr("192.168.123.99"); 
 address.sin_port = htons(9734);
 len = sizeof(address);
 result = connect(sockfd, (struct sockaddr *)&address, len);

 if(result == -1) {
  perror("oops: client1");
  exit(1);
 }
 while(strcmp(put_string,"end")){
  char get_string[255];
  scanf(" %s",put_string); 
  write(sockfd, put_string, sizeof(put_string));

  if(!strcmp("put",put_string)){
   printf("file name?\n");
   scanf(" %s",put_string);
   write(sockfd, put_string, sizeof(put_string));
   file=open(put_string,O_RDONLY);
   if(file<1 data-blogger-escaped-buf="" data-blogger-escaped-close="" data-blogger-escaped-continue="" data-blogger-escaped-done="" data-blogger-escaped-error="" data-blogger-escaped-exit="" data-blogger-escaped-file="" data-blogger-escaped-get_string="" data-blogger-escaped-memset="" data-blogger-escaped-pre="" data-blogger-escaped-put_string="" data-blogger-escaped-read="" data-blogger-escaped-sizeof="" data-blogger-escaped-sockfd="" data-blogger-escaped-while="" data-blogger-escaped-write=""> 
서버 부분
  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define FLAG_NORMAL 0
#define FLAG_IS_FLIE_EXIST 1
#define FLAG_TRANSFER 2
int main()
{
 int server_sockfd, client_sockfd;
 int flag=FLAG_NORMAL;
 int file;
 int buf;
 int server_len, client_len;
 char file_name[100],remove[103];
 struct sockaddr_in server_address;
 struct sockaddr_in client_address;
 int result;
 fd_set readfds, testfds;

  char message[255]={0};
 /*socket creat & naming*/
 server_sockfd= socket(AF_INET, SOCK_STREAM,0);
 server_address.sin_family = AF_INET;
 server_address.sin_addr.s_addr = htonl(INADDR_ANY);
 server_address.sin_port = htons(9734);
 server_len = sizeof(server_address);

 /*bind*/
 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

 /*listen*/
 listen(server_sockfd, 5);

 FD_ZERO(&readfds);
 FD_SET(server_sockfd, &readfds);

 while(1){
  int fd;
  int nread;
  testfds=readfds;

  result=select(FD_SETSIZE, &testfds, (fd_set *)0 ,(fd_set *)0, (struct timeval *)0);

  if(result < 1)
  {
   perror("server error:");
   exit(1);
  }
  for(fd =0; fd < FD_SETSIZE; fd++)
  {
   if(FD_ISSET(fd,&testfds)){
    if(fd == server_sockfd){
     client_len = sizeof(client_address);
     client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len);
     FD_SET(client_sockfd, &readfds);
     printf("adding client on fd %d\n", client_sockfd);
    }
    else
    {
     ioctl(fd, FIONREAD, &nread);

     if(nread ==0)
     {
      close(fd);
      FD_CLR(fd, &readfds);
      printf("removing client on fd %d\n",fd);
     }
     else
     {
      memset(message,0,sizeof(message));
      read(fd, message, sizeof(message));
      printf("%s\n",message);
      if(strncmp("put",message,strlen(message))==0){
        read(fd, message, sizeof(message));
        file=open(message,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU);
        strcpy(file_name,message);
        if(file<1 data-blogger-escaped-6="" data-blogger-escaped-break="" data-blogger-escaped-buf="" data-blogger-escaped-close="" data-blogger-escaped-done="" data-blogger-escaped-else="" data-blogger-escaped-error="" data-blogger-escaped-fd="" data-blogger-escaped-file="" data-blogger-escaped-file_name="" data-blogger-escaped-if="" data-blogger-escaped-isolation="" data-blogger-escaped-memset="" data-blogger-escaped-message="" data-blogger-escaped-n="" data-blogger-escaped-pre="" data-blogger-escaped-printf="" data-blogger-escaped-remove="" data-blogger-escaped-rm="" data-blogger-escaped-s="" data-blogger-escaped-sizeof="" data-blogger-escaped-sprintf="" data-blogger-escaped-strcmp="" data-blogger-escaped-strlen="" data-blogger-escaped-system="" data-blogger-escaped-while="" data-blogger-escaped-write="">  
많은 예외처리를 무시했지만 동작은 되니까 ..ㅎㅎ

댓글 없음:

댓글 쓰기