Linux--UDP编程-linux-server-windows-client

UDP信息传输

Linux服务器

/************linux-udp-image-server*****************/
/**
* 这是一个linux 服务器端程序,不停的接收数据并显示出来
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/time.h>

#define SER_PORT 3000  
#define MAX_DATA 51200  //50kb  

#ifndef bool  
#define bool int  
#define true 1  
#define false 0  
#endif



int main(int argc,char* argv[]){

        int serv_sock;/*服务器端socket描述符*/
        int clnt_addr_len;
        int recv_len;

        char message[MAX_DATA];

        struct  sockaddr_in serv_addr,clnt_addr;/*服务器地址与客户端地址*/

        serv_sock = socket(AF_INET, SOCK_DGRAM, 0);
        if(serv_sock < 0)
        {
                printf("UDP socket creation error!\n");
                exit(1);
        }
        else
        {
                printf("UDP socket is creat successful!\n");

        }

        bzero(&serv_addr, sizeof(serv_addr));
        serv_addr.sin_family=AF_INET;
        serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
        serv_addr.sin_port=htons(SER_PORT);

        if(bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
        {
                printf("bind() error!\n");
                exit(1);
        }
        else
        {
                printf("bind is success!\n");

        }
        while(1)
        {
                clnt_addr_len=sizeof(clnt_addr);
                recv_len=recvfrom(serv_sock, message, MAX_DATA, 0, (struct sockaddr*)&clnt_addr, &clnt_addr_len);
                printf("%s\n",message);
                sendto(serv_sock, message, recv_len, 0, (struct sockaddr*)&clnt_addr, clnt_addr_len);
        }
        close(serv_sock);
        return 0;
}

windows客户端

/********************windows-client******************/

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>

#define SERV_PORT 3000  
#define MAX_DATA 51200  //50kb  
#define FILE_LENGTH 921654 //图片大小

#pragma comment(lib,"ws2_32.lib")

#ifndef bool  
#define bool int  
#define true 1  
#define false 0  
#endif

int main(int argc, char* argv[]) {
    WORD socketVersion = MAKEWORD(2, 2);
    WSADATA wsaData;
    SOCKADDR_IN sin;
    SOCKADDR_IN addr_client;
    SOCKET hSocket;
    int client_len;
    int recv_num;
    int send_num;
    char recv_buf[20];
    char send_path[1024];
    char send_over[9] = "send over";
    char send_buf[MAX_DATA];
    FILE *fd = NULL;

    if (WSAStartup(socketVersion, &wsaData) != 0)
    {
        return 0;
    }


    hSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (hSocket == INVALID_SOCKET)
        printf("socket() error!");

    sin.sin_family = AF_INET;
    sin.sin_port = htons(SERV_PORT);
    sin.sin_addr.S_un.S_addr = inet_addr("101.200.46.138");
    int len = sizeof(sin);

    int nNetTimeout = 2000;//设置超时
    if (SOCKET_ERROR == setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&nNetTimeout, sizeof(int)))
    {
        printf("Set Ser_RecTIMEO error !\r\n");
        exit(1);
    }

    while (1) {//不停的发
        char * sendData = "from yuanlifu!";
        sendto(hSocket, sendData, strlen(sendData), 0, (SOCKADDR*)&sin, len);

        char recvData[255];
        int ret = recvfrom(hSocket, recvData, 255, 0, (SOCKADDR *)&sin, &len);
        if (ret > 0)
        {
            recvData[ret] = 0x00;
            printf(recvData);
        }
    }

    closesocket(hSocket);
    WSACleanup();
    return 0;
}

——————————————————————————

UDP文件传输

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页