c - 为了将 tcp 程序转换为 udp 需要进行哪些更改

标签 c sockets tcp udp client-server

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<poll.h>
#include<unistd.h>
#include<arpa/inet.h>

int main()
{
    struct pollfd fdarray[5];
    int sfd,port,nsfd,n,clen,ret,i;
    char buff[100];
    struct sockaddr_in sadd,cadd;
    memset(buff,0,sizeof(buff));
    sfd=socket(AF_INET,SOCK_STREAM,0);
    if(sfd<0)
    {
        printf("sorry unable to open the file");
        exit(1);
    }
    memset(&sadd,0,sizeof(sadd));
    sadd.sin_port=htons(3503);
    sadd.sin_family=AF_INET;
    sadd.sin_addr.s_addr=INADDR_ANY;
    if(bind(sfd,(struct sockaddr*) &sadd,sizeof(sadd))<0)
    {
        printf("errortttt");
    //close(sfd);
        exit(0);
    }
    int r;
    r=listen(sfd,5);
    if(r<0)
    {
        perror("error");
    }
    memset(fdarray,0,sizeof(fdarray));
    fdarray[0].fd=sfd;
    fdarray[0].events=POLLIN;
    clen=sizeof(cadd);
    int rc,nfds=1;
    printf("waiting on poll\n");
    while(1){
        rc=poll(fdarray,nfds,-1);
        if(rc<0)
        {
            perror("poll failed");
        }
        int currentsize=nfds;
        for(i=0;i<currentsize;i++)  
        {
            if(fdarray[i].revents==0)
            continue;
        else if(fdarray[i].fd==sfd)
        {
            printf("socket is reading");
            nsfd=accept(sfd,NULL,NULL);
            printf("new connection is established");
            fdarray[nfds].fd=nsfd;
            fdarray[nfds].events=POLLIN;
            nfds++;

        }
        else
        {
            printf("%d",fdarray[i].fd);


            recv(fdarray[i].fd,buff,sizeof(buff),0);
            int j=0;
            for( j=1;j<nfds;j++)
            {
                if(j!=i)    
                send(fdarray[j].fd,buff,sizeof(buff),0);
        }
    }
}
}

return 0;
}

我是新网络,这是聊天服务器,这意味着假设有 10 个客户端都连接到服务器,当其中一个客户端发送消息时,服务器应该将消息发送给除正在发送的客户端之外的所有客户端我使用用 tcp 编写的轮询程序完成了此操作,为了转换为 udp 需要进行哪些更改以及如何在 udp 中轮询,因为没有 nsfd,即意味着接受调用

#include "libsock"
int sfd=0;

* sender(void* dummy)
{
    char buf[512];
    int len=1;
    int i;
    while(len>0){
        for(i=0;i<512;i++)
        buf[i]='\0';
    fgets(buf,512,stdin);
    len=send(sfd,buf,strlen(buf)+1,0);
}
}

void* receiver(void* dummy)
{
    int len=1;
    char buf[512];
    int i;
    while(len>0)
    {
        for(i=0;i<512;i++)
        buf[i]='\0';
    len=recv(sfd,buf,512,0);
    printf("%s",buf);
}
}

int main()
{

    sfd=socket(AF_INET,SOCK_STREAM,0);
    if(sfd<0)
    {
        printf("socket error.\n");
        return 0;
    }
    struct sockaddr_in serv;
    serv.sin_family=AF_INET;
    serv.sin_port=htons(3503);
    inet_pton(AF_INET,"127.0.0.1",&serv.sin_addr);
    if(connect(sfd,(struct sockaddr*)&serv,sizeof(serv))<0)
    return 0;
    pthread_t s,r;
    pthread_create(&s,NULL,sender,NULL);
    pthread_create(&r,NULL,receiver,NULL);
    pthread_join(s,NULL);
    return 0;
}

最佳答案

如果您只想发送单个数据包,而没有 TCP 提供的通常保证,那么您可以将程序更改为简单的 recvfrom-client/server (这部分对于每个操作系统都非常相似),这里是more info .

但是如果您想要这些保证,(1)您的数据包以正确的顺序到达(2)并且(3)完好无损,那么您实际上必须自己重新实现 TCP 。 (有时会对游戏执行此操作,因为它们通常不关心旧数据)您必须使客户端和服务器重新发送丢失/损坏的包。因此必须对包进行编号和校验,但棘手的部分是确定每个包应缓冲数据多长时间。

关于c - 为了将 tcp 程序转换为 udp 需要进行哪些更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15120642/

相关文章:

c - 打印我的 IP TCP 服务器 C

c - tcp时间戳,记录返回回复所需的时间

c - 初始化指向结构的指针

c - C中的typedef结构问题

在 ubuntu 20.04.3 上从 gdb 运行 helloworld.c 的 CRC 不匹配警告

c - 使用 i++(或++i)真的是一个好习惯吗?

C++ 创建多个套接字客户端

c++ - 在 C++ 中,关于位移和转换数据类型

c++ - 如何确保从 tcp/ip 套接字获取最新数据?

c - 正在为服务器设置奇怪的 IP 地址 `0.0.0.0`。不确定它是否有效