歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Linux 網絡編程---->簡單的UDP實例

這是一個簡單的UDP的實例而已~

//!> server
//!> server.c




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

#define     SERV_PORT   6000
#define   MAXLINE       1024

int main( int argc, char ** argv )
{
    char   recv[MAXLINE];
    int       listenfd,connfd;
    int       chilen, len,i;
    int       nread;
    structsockaddr_in servaddr, chiaddr;
   
   //!> establish the socket
   //!>
    if( (listenfd = socket(  AF_INET, SOCK_DGRAM,0  ) ) == -1 )
    {
      printf("Socket error.. %d\n", errno);
       exit(EXIT_FAILURE );
    }
   
   //!>
   //!> write info
    bzero(&servaddr, sizeof( servaddr ) );
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr  =htonl( INADDR_ANY);
   servaddr.sin_port = htons( SERV_PORT );
   
   //!>
   //!> bind
    if( bind(listenfd, ( struct sockaddr * )&servaddr, sizeof(servaddr ) ) == -1 )
    {
       printf("Binderror... %d\n", errno);
       exit(EXIT_FAILURE );
    }
   
    while( 1)
     
       chilen =sizeof( chiaddr );
       
       memset( recv, 0, sizeof( recv ) );
        nread =recvfrom(connfd,recv, MAXLINE, 0, (struct sockaddr*)&chiaddr, &chilen);//read(connfd, recv, MAXLINE );
                                                               //!> 記住:一般都用它
       if( nread ==-1 )
       {
         continue;
          printf("Readerror ... %d\n", errno);
          close(connfd );
          exit(EXIT_FAILURE );
       }
       else if(nread == 0 )
       {
          printf("Haveno data..\n");
         continue;                           //!> return
       }
       else
       {
          len =sizeof( recv );
          recv[len] ='\0';
         printf("Server get from client : %s\n", recv);
         
          i = 0;
          while(recv[i] )            //!> 此處是一個簡單的處理,將小寫字母改成大寫回送
          {
             recv[i] =toupper( recv[i] );
             i++;
          }
            
         sendto(connfd, recv, strlen( recv ), 0, (struct sockaddr*)&chiaddr, chilen);
               //!> send to client
       }
      
    }
   
    return0;
}


//!> client
//!>client.c




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

#define    SERV_PORT   6000
#define   MAXLINE       1024

int main( int argc, char ** argv )
{
    char   recv[MAXLINE];
    int      connfd;
    int       nread;
   int       len;
    structsockaddr_in servaddr;
   
    if( argc !=2 )
    {
      printf("Please input the server's IP only !");
       exit(EXIT_FAILURE );
    }
   
   //!>
   //!> socket
    if( ( connfd= socket(  AF_INET, SOCK_DGRAM,0  ) ) == -1 )
    {
      printf("Socket error.. %d\n", errno);
       exit(EXIT_FAILURE );
    }
   
   //!>
   //!> info
   bzero(&servaddr, sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(SERV_PORT);
     inet_pton(AF_INET, argv[1] ,&servaddr.sin_addr);
     
   
    while( 1)
    {
      
       //write(connfd, "hello\n", 7 );
      
       memset(recv, 0, sizeof( recv ) );
      sendto(connfd, "hello\n", 7, 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
      printf("Client send : hello\n");
      
       sleep( 2);
   
       len =sizeof( servaddr );
       nread =recvfrom(connfd, recv, MAXLINE, 0, (struct sockaddr*)&servaddr, &len);//read( connfd,recv, MAXLINE );
      
       if( nread ==-1 )
       {
          printf("Readerror.. %d\n", errno);
          exit(EXIT_FAILURE );
       }
       else if(nread == 0 )
       {
         continue;
       }
       else
       {
          len =strlen( recv );
          recv[len] ='\0';
         printf("Client get from server : %s\n", recv);
       }
      
    }

    close(connfd );
      
    return0;
}
Copyright © Linux教程網 All Rights Reserved