zoukankan      html  css  js  c++  java
  • 简单的UDP回射程序

    Server:

    #include <stdio.h>
    #include
    <string.h>
    #include
    <arpa/inet.h>
    #include
    <netinet/in.h>
    #include
    <sys/socket.h>
    #define MAXSIZE 1024
    #define SERV_PORT 2000
    void dg_echo(int sockfd, struct sockaddr * pcliaddr, socklen_t clilen)
    {
    int n;
    socklen_t len;
    while(1)
    {
    char mesg[MAXSIZE];
    //回射机制
    len=clilen;
    n
    =recvfrom(sockfd,mesg,MAXSIZE,0,pcliaddr,&len);
    char servermsg[MAXSIZE]="From Server: ";
    int l = strlen(servermsg);
    servermsg[l]
    ='\0';
    strcat(servermsg,mesg);
    sendto(sockfd,servermsg,n
    +l,0,pcliaddr,len);
    }
    }
    int main( int args, char ** argv)
    {
    int sockfd;
    struct sockaddr_in serveraddr, clientaddr;
    sockfd
    =socket(AF_INET,SOCK_DGRAM,0);
    bzero(
    &serveraddr,sizeof(serveraddr));
    serveraddr.sin_family
    =AF_INET;
    serveraddr.sin_addr.s_addr
    =htonl(INADDR_ANY);
    serveraddr.sin_port
    =htons(SERV_PORT);
    bind(sockfd,(
    struct sockaddr *)&serveraddr,sizeof(serveraddr));
    dg_echo(sockfd,(
    struct sockaddr *)&serveraddr,sizeof(clientaddr));
    return 0;
    }

    Client:

    #include <stdio.h>
    #include
    <string.h>
    #include
    <arpa/inet.h>
    #include
    <netinet/in.h>
    #include
    <sys/socket.h>
    #define MAXSIZE 1024
    #define SERVER_PORT 2000
    void dg_client (FILE* fp,int sockfd, const struct sockaddr * pservaddr,socklen_t serverlen)
    {
    int n;
    char sendline[MAXSIZE],recvline[MAXSIZE+1];
    printf(
    "From Client: ");
    while(fgets(sendline,MAXSIZE,fp)!=NULL)
    {
    sendto(sockfd,sendline,strlen(sendline),
    0,pservaddr,serverlen);
    n
    =recvfrom(sockfd,recvline,MAXSIZE,0,NULL,NULL);
    recvline[n]
    ='\0';
    fputs(recvline,stdout);
    printf(
    "From Client: ");
    }
    }
    int main (int args, char ** argv )
    {
    int sockfd;
    struct sockaddr_in serveraddr;
    bzero(
    &serveraddr,sizeof(serveraddr));

    serveraddr.sin_family
    =AF_INET;
    serveraddr.sin_port
    =htons(SERVER_PORT);
    inet_pton(AF_INET,
    "10.3.1.215",&serveraddr.sin_addr);
    sockfd
    =socket(AF_INET,SOCK_DGRAM,0);
    dg_client(stdin,sockfd,(
    struct sockaddr * )&serveraddr,sizeof(serveraddr));
    return 0;

    }

  • 相关阅读:
    Spark SQL join的三种实现方式
    BitMap算法
    mysql索引建立原则
    Java常用容器对比
    如何正确的停止一个线程
    读书笔记——凤凰架构
    Linux command 整理
    禁足期间,好好学习,好好休息
    Oracle SQL Update
    Shell 知识总结
  • 原文地址:https://www.cnblogs.com/coser/p/1968810.html
Copyright © 2011-2022 走看看