Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1264719
  • 博文数量: 404
  • 博客积分: 10011
  • 博客等级: 上将
  • 技术积分: 5382
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-03 16:29
文章存档

2010年(40)

2009年(140)

2008年(224)

我的朋友

分类: LINUX

2009-05-25 10:59:39

先看Server端的代码:
[code]
#include
#include
#include
#include

const int MYPORT=4950; // the port users will be connecting to
const int MAXBUFLEN=100;


void init_win_socket();
void clean_win_socket();
void close_win_socket(SOCKET &soc_handle);

//this socket is intend to use as a simple server
int main()
{
    puts("start main server....");
   
    //init windows socket
    init_win_socket();
 
    SOCKET main_sock_fd;
    struct sockaddr_in main_soc_addr; // my address information
    struct sockaddr_in client_soc_addr; // connector's address information
   
    int addr_len;
    int numbytes;
    char buf[MAXBUFLEN];
    int ret=0;
    //create the sock
    if ((main_sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
       puts("CREATE SOCK ERROR:");
    }
   
    //configure sockaddr_in
    main_soc_addr.sin_family = AF_INET; // host byte order
    main_soc_addr.sin_port = htons(MYPORT); // short, network byte order
    main_soc_addr.sin_addr.s_addr= htonl(INADDR_ANY); // automatically fill with my IP
    memset(main_soc_addr.sin_zero, '\0', sizeof (main_soc_addr.sin_zero));
   
    //bind the socket
    ret = bind(main_sock_fd, (struct sockaddr *)&main_soc_addr, sizeof main_soc_addr);
    if ( ret == -1)
    {
     puts("BIND SOCK ERROR:");
    }
   
    //get data from client socket
    addr_len = sizeof client_soc_addr;
    numbytes = recvfrom(main_sock_fd, buf, MAXBUFLEN-1 , 0,
                                           (struct sockaddr *)&client_soc_addr, &addr_len);
    if (numbytes == -1)
    {
        puts("RECEIVE SOCK ERROR:");
    }
    else
    {
        printf("got packet from %s\n",inet_ntoa(client_soc_addr.sin_addr));
        printf("packet is %d bytes long\n",numbytes);
        buf[numbytes] = '\0';
        printf("packet contains \"%s\"\n",buf);
    }
    //close and clean the sockets
    close_win_socket(main_sock_fd);
    clean_win_socket();
    
    puts("any key to continue...");
    getchar(); 
    return 0; 
}

void init_win_socket()
{
     WSADATA wdata;
     if(WSAStartup(MAKEWORD(2,2), &wdata)!=0)
     {
      puts("WAS ERROR:");
     }   
}

void close_win_socket(SOCKET &soc_handle)
{
     if(soc_handle>0)
     {
          closesocket(soc_handle);
     }
}
void clean_win_socket()
{
     WSACleanup();
}
 
#include
#include
#include
#include

const int SERVERPORT= 4950; // the port users will be connecting to
const int MYPORT=4900; // the local port
#define SERVER_IP "192.168.136.93"

void init_win_socket();
void clean_win_socket();
void close_win_socket(SOCKET &soc_handle);

//this socket is intend to use as a simple client
int  main()
{
    puts("start main client...");
    int numBytes=0;
    int ret=0;
    char sendBuf[]="Hello, this is used to test UDP socket!\0";
   
    //init windows socket
    init_win_socket();
   
    SOCKET local_sock_fd;
    struct sockaddr_in main_soc_addr;
    struct sockaddr_in client_soc_addr;
   
    //create the client socket
    local_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
    if(local_sock_fd ==-1)
    {
         puts("CREATE SOCK ERROR:");
    }
    //configure the client's local socket addr
    client_soc_addr.sin_family = AF_INET;
    client_soc_addr.sin_port = htons(MYPORT);
    client_soc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    memset(client_soc_addr.sin_zero, '\0', sizeof(client_soc_addr.sin_zero));
   
    //bind the socket
    ret = bind(local_sock_fd, (struct sockaddr *)&client_soc_addr, sizeof(client_soc_addr));
    if(ret==-1)
    {
         puts("BIND SOCK ERROR:");
     }
    
     //configure the client's server socket addr
     main_soc_addr.sin_family = AF_INET;
     main_soc_addr.sin_port = htons(SERVERPORT);
     main_soc_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
     memset(main_soc_addr.sin_zero, '\0', sizeof(main_soc_addr.sin_zero));
    
    //now,we can send data to the server
    numBytes =sendto(local_sock_fd, sendBuf, strlen(sendBuf),
                                   0, (struct sockaddr*)&main_soc_addr, sizeof(sockaddr));
    if(numBytes ==-1)
    {
      puts("SEND DATA ERROR:");
    }
   
    //close and clean the windows sockets
    close_win_socket(local_sock_fd);
    clean_win_socket();
    
    puts("any key to continue...");
    getchar(); 
    return 0;
}

//init windows socket
void init_win_socket()
{
     WSADATA wdata;
     if(WSAStartup(MAKEWORD(2,2), &wdata)!=0)
     {
      puts("WAS ERROR:");
     }   
}

//close windows socket
void close_win_socket(SOCKET &soc_handle)
{
     if(soc_handle>0)
     {
          closesocket(soc_handle);
     }
}

//clean windows socket
void clean_win_socket()
{
     WSACleanup();
}

阅读(1684) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~