基于UDP协议的信息传输

原创 2015年07月09日 17:20:13


这是简单的客户端到服务端的信息传输,目前只支持一传一回

客户端代码:

//chang. exercise of UDP socket.  
// Implements the function of send info between server and client.  
//When this server is listening and a client send an request, it's will print the info on the cmd and reply a data:"OK".  
//client.  
#include "Winsock2.h"  
#include "iostream"  
#include "stdio.h"  
#pragma comment(lib, "ws2_32.lib")   
#define HOST_IP "222.196.200.135"  
#define HOST_PORT 8080   
void main(){  
    //the version bit of Winsock  
    int version_a = 1;//low bit  
    int version_b = 1;//high bit  
 
    //makeword  
    WORD versionRequest = MAKEWORD(version_a,version_b);  
    WSAData wsaData;  
    int error;  
    error = WSAStartup(versionRequest, &wsaData);    
    if(error != 0   ){  
        printf("ERROR!");  
        return;  
    }  
    //check whether the version is 1.1, if not print the error and cleanup wsa?  
    if (LOBYTE(wsaData.wVersion)  != 1 || HIBYTE(wsaData.wVersion) != 1)  
    {  
        printf("WRONG WINSOCK VERSION!");  
        WSACleanup();  
        return;  
    }  
 
 
    //request info obtained by user's input  
    char requestStr[100];  
 
    //build a sockeet   
    SOCKET socClient = socket(AF_INET, SOCK_DGRAM, 0);  
    SOCKADDR_IN addrSrv;        // a instance of SOCKADDR_IN, which is used in format of SOCKADDR.  
    addrSrv.sin_addr.S_un.S_addr=inet_addr("222.196.200.135");        //set the host IP  
    addrSrv.sin_family=AF_INET;     //set the protocol family  
    addrSrv.sin_port=htons(HOST_PORT);      //set the port number  
 
    // array to store the data that server feedback.  
    char cRecvBuf[100];  
  int len=sizeof(SOCKADDR);  
 
    //while   
    while(true){  
	fflush(stdin);
            // gain info that user input    
	gets(requestStr);
        //sendto function is used to send programe data to the server  
       sendto(socClient, requestStr, strlen(requestStr)+1, 0, (SOCKADDR*) &addrSrv, sizeof(SOCKADDR));  
 
        //receive the feedback info from server  
        //recv function is used in client.  
        //recv(socClient, cRecvBuf, strlen(cRecvBuf)+1, 0); 
	recvfrom(socClient, cRecvBuf, 100, 0,  (SOCKADDR*) &addrSrv, &len); 
        //output the cRecvBuf to cmd  
        std::cout<< cRecvBuf << std::endl;  
    }  
      
    //close the socket and cleanup the wsadata  
    closesocket(socClient);  
    WSACleanup();  
 
//  return 0;  
} 

服务端:

//chang. exercise of UDP socket.  
// Implements the function of send info between server and client.  
//When this server is listening and a client send an request, it's will print the info on the cmd and reply a data:"OK".  
//server.  
#include "stdio.h"  
#include "Winsock2.h"  
#include "iostream"  
#include "string"  
//should add the under line if not add lib in the project.(appendicular dependent)  
#pragma comment(lib, "ws2_32.lib")   
//define host IP and usable port.  
//#define HOST_IP 127.0.0.1  
#define HOST_IP 222.196.200.135 
#define HOST_PORT 8080  
#define OK_STR "OK"   
void main(){  
    //the version bit of Winsock  
    int version_a = 1;//low bit  
    int version_b = 1;//high bit  
 
    //makeword  
    WORD versionRequest = MAKEWORD(version_a,version_b);  
    WSAData wsaData;  
    int err;  
    //wsa startup  
    err = WSAStartup(versionRequest, &wsaData);    
    if(err != 0 ){  
        printf("ERROR!");  
        return;  
    }  
    //check whether the version is 1.1, if not print the error and cleanup wsa?  
    if (LOBYTE(wsaData.wVersion)  != 1 || HIBYTE(wsaData.wVersion) != 1)  
    {  
        printf("WRONG WINSOCK VERSION!");  
        WSACleanup();  
        return;  
    }  
 
    /*  
    *build the socket  
    *first param is protocol family, generally AF_INET for IP internet.  
    *second param is type of socket, SOCK_DGRAM is for Data Gram(UDP), and SOCK_STREAM is for TCP.  
    *the last param is communicate protocol. Generally is zero.  
    */ 
    SOCKET socServer = socket(AF_INET, SOCK_DGRAM,0);  
    //infomation of address, always NOT being operated directly.  
    SOCKADDR_IN addr_Srv;  
    //Struct sin_addr is  used to defind IP address,  it's a property of addr_in.  
    //It's nest three structs as S_un_b, S_un_w and S-un.(union)  
    addr_Srv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);  
    //set protocol family  
    addr_Srv.sin_family = AF_INET;  
    //set host port  
    addr_Srv.sin_port = htons(HOST_PORT);  
     //bind socket to  the host  
    bind(socServer,( SOCKADDR*) &addr_Srv, sizeof(SOCKADDR));  
 
    //address  of a client  
    SOCKADDR_IN addr_Clt;  
    char recvBuf[100];  
 	char words[100];
    int fromlen = sizeof(SOCKADDR);  
    // while() to listen all the time  
    while(true){  
	fflush(stdin);
        //receive data function of server  
        recvfrom(socServer, recvBuf, 100, 0,  (SOCKADDR*) &addr_Clt, &fromlen);  
        //output the data received to cmd  
        std::cout<<recvBuf<<std::endl;  
        //send "OK" to the from_client to indicates that data have been received.  
        //send function is used by server, while sendto function is used by client. NOT sure.  
//      send(addr_Clt, OK_STR,  strlen(OK_STR)+1, 0);  
	gets(words);
        sendto(socServer, words, strlen(words)+1, 0, (SOCKADDR*)&addr_Clt, sizeof(SOCKADDR));  
    }  
      
    // at last close the socket.  
    closesocket(socServer);  
    //cleanup WSAData  
    WSACleanup();} 


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Java学习笔记 UDP协议传输信息

在网络使用中,我们有两种常用的方法传输信息,一种是通过TCP协议,一种是通过UDP协议,这一次,我们来使用UDP协议进行一次信息传输。 同样,我们拥有两个类,一个是服务器类,一个是客户端类,客户端用...

【tcp-ip学习总结】传输层协议UDP和TCP

关于端口 互联网中任何两台主机之间通讯,都是从一端到另一端,又称端到端的通讯。 端口又是什么? 现代的操作系统都装有很多个应用程序,信息传到了我的电脑上我怎么知道这个信息就是传给我的QQ应用程序...
  • ttf1993
  • ttf1993
  • 2015年04月21日 19:39
  • 614

udp协议结合飞秋传输

  • 2015年06月13日 11:53
  • 2KB
  • 下载

基于UDP高性能传输协议UDT doc翻译(三)

原文转自:http://hi.baidu.com/doodlezone/item/42fe03cd5b06f53598b498c2 方法名 connect方法 功能 用于连...
  • pingD
  • pingD
  • 2013年11月15日 21:23
  • 2735

UDP协议的VC++文本传输

  • 2008年05月20日 17:27
  • 39KB
  • 下载

基于UDP协议的简单基本视频传输程序的编写

一、UDP协议 UDP是User Datagram Protocol的简称,中文名是用户数据报协议,是OSI参考模型中一种无连接的传输层协议。正式通信前不必与对方先建立连接,直接向接收方发送数据,是一...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:基于UDP协议的信息传输
举报原因:
原因补充:

(最多只允许输入30个字)