socket基本函數用法

寫了個很簡單的socket通信例子,client和server端分別起兩個thread,一個用於發送一個用於接收數據。


/*

* client.c

*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<pthread.h>

/*
* tidsend func
*/
void *send_func(void *arg)
{
   printf("process client Thread tidSend is start!\n");
   char wbuf[128];
   int sock = *(int *)arg;
   printf("Thread tidsend sock = %d \n",sock);
   while(1)
   {
       scanf("%s",wbuf);
       int wsize = send(sock,wbuf,sizeof(wbuf),0);
       if(wsize<=0)
       {
           perror("write error:");
           close(sock);
           break;
       }    
   }    
}

/*
* tidRev func
*/
void *rec_func(void *arg)
{
   printf("process client Thread tidRev is start!\n");
   char rbuf[128];
   int sock = *(int *)arg;
   printf("Thread tidRev sock = %d \n",sock);
   while(1)
   {
       int    rsize = recv(sock,rbuf,sizeof(rbuf),0);
       if(rsize > 0)
       {
           rbuf[rsize]='\0';
           printf("recv msg from server: %s\n",rbuf);
       }
       if(rsize<0)
       {
           close(sock);                
           perror("read error:");
           break;
       }
   }
}


int main(int argc,char **argv)
{      
   int sock;
   struct sockaddr_in    server = {0};
   struct timeval timeo;

   timeo.tv_sec  = 0;
   timeo.tv_usec = 1000 * 1000;        //
   socklen_t len = sizeof(timeo);
   if( argc != 2)
   {
       printf("usage: ./client <ipaddress>\n");
       return -1;
   }

   sock = socket(AF_INET, SOCK_STREAM, 0);
   if(sock < 0)
   {
       perror("Create TCP Socket");
       return -1;
   }

   setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeo, len);

   server.sin_family = AF_INET;
   server.sin_port = htons(30000);
   inet_pton(AF_INET,  argv[1], &(server.sin_addr));

   int res = connect(sock, (struct sockaddr*)&server, sizeof(server));
   if (res < 0) // connect failure
   {
       if(res == EINPROGRESS)
       {
           perror("connecting stream socket time out:");
       }
       else
       {
           perror("connecting stream socket error:");
       }
       close(sock);
       return -1;
   }
   printf("Connect Server@%s\n",argv[1]);
   /* connect sucess */
   pthread_t tidSend;
   pthread_t tidRev;
   void *tret;
   // create thread tidsend    
   if (pthread_create(&tidSend,NULL,send_func,(void *)&sock)!=0)
   {
       printf("Create thread 1 error!\n");
       exit(1);    
   }    

   // create thread tidRev
   if (pthread_create(&tidRev,NULL,rec_func,(void *)&sock)!=0)
   {
       printf("Create thread 2 error!\n");        
       exit(1);    
   }

   // wait for the thread tidSend finish
   if (pthread_join(tidSend,&tret)!=0)
   {        
       printf("Join thread 1 error!\n");
       exit(1);
    }

    // wait for the thread tidRev finish
    if (pthread_join(tidRev,&tret)!=0)
    {
        printf("Join thread 2 error!\n");
        exit(1);
     }    
     close(sock);
     return 0;
}


/*

* server.c

*/


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<pthread.h>


/*
* tidsend func
*/
void *send_func(void *arg)
{
   printf("process sever Thread tidSend is start!\n");
   char wbuf[128];
   int connfd = *(int *)arg;
   while(1)
   {
    scanf("%s",wbuf);
       int n = send(connfd, wbuf, sizeof(wbuf),0);
 printf("n = %d\n",n);
       if(n <= 0)
       {
           perror("send error:");
           close(connfd);
           break;
       }
   }
}

/*
* tidRev func
*/
void *rec_func(void *arg)
{
   printf("process server Thread tidRev is start!\n");
   char rbuf[128];
   int connfd = *(int *)arg;
   while(1)
   {
       int n = recv(connfd, rbuf, sizeof(rbuf), 0);
 if(n <= 0)
 {
  perror("recv error:");
           close(connfd);
           break;
 }          
       rbuf[n] = '\0';
       printf("recvmsg from client: %s\n", rbuf);      
   }


}

int main(int argc, char** argv)
{
   int    listenfd, connfd;
   struct sockaddr_in     servaddr = {0};

   if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
   {
       perror("Create TCP Socket");
       return -1;
   }

   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
   servaddr.sin_port = htons(30000);

   if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)
   {
       perror("bind socket error:");
       return -1;
   }

   if( listen(listenfd, 20) == -1)
   {
       perror("listen socket error:");
       return -1;
   }

   printf("======waiting for client's request======\n");
   struct sockaddr_in RemoteSockAddr;
   int remotesize = sizeof(struct sockaddr_in);

   if( (connfd = accept(listenfd, (struct sockaddr*)&RemoteSockAddr, &remotesize)) == -1)
   {
 perror("accept socket error:");
       exit(1);
   }
printf("source ip = %x,port = %d \n",ntohl(RemoteSockAddr.sin_addr.s_addr),htons(RemoteSockAddr.sin_port));

pthread_t tidSend;
   pthread_t tidRev;
   void *tret;
   // create thread tidsend    
   if (pthread_create(&tidSend,NULL,send_func,(void *)&connfd)!=0)
   {
       printf("Create thread 1 error!\n");
       exit(1);    
   }    

   // create thread tidRev
   if (pthread_create(&tidRev,NULL,rec_func,(void *)&connfd)!=0)
   {
       printf("Create thread 2 error!\n");        
       exit(1);    
   }

   // wait for the thread tidSend finish
   if (pthread_join(tidSend,&tret)!=0)
   {        
       printf("Join thread 1 error!\n");
       exit(1);
   }

    // wait for the thread tidRev finish
   if (pthread_join(tidRev,&tret)!=0)
   {
       printf("Join thread 2 error!\n");
       exit(1);
   }
   close(listenfd);
   return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章