Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编写同时处理TCP和UDP服务器的服务器时出现问题_C_Sockets_Unix_Tcp_Udp - Fatal编程技术网

编写同时处理TCP和UDP服务器的服务器时出现问题

编写同时处理TCP和UDP服务器的服务器时出现问题,c,sockets,unix,tcp,udp,C,Sockets,Unix,Tcp,Udp,我正在编写一个服务器程序,作为处理TCP和UDP客户端的任务的一部分。我是通过选择函数写的。我的TCP服务器很好,但我的UDP服务器工作不正常 每个服务器逻辑的第一行是“正确到达的UDP/TCP服务器部分” TCP服务器中的行打印正确,但UDP部分打印不正确 该程序的主要目的是给定一个书名,我将在文件中搜索并显示authorname。我必须同时处理tcp和udp部分。提前谢谢 int main(int argc, char **argv) { char *line=(char*)malloc(

我正在编写一个服务器程序,作为处理TCP和UDP客户端的任务的一部分。我是通过选择函数写的。我的TCP服务器很好,但我的UDP服务器工作不正常

每个服务器逻辑的第一行是“正确到达的UDP/TCP服务器部分” TCP服务器中的行打印正确,但UDP部分打印不正确

该程序的主要目的是给定一个书名,我将在文件中搜索并显示authorname。我必须同时处理tcp和udp部分。提前谢谢

 int main(int argc, char **argv)
{
char *line=(char*)malloc(sizeof(char)* MAXLINE); //line contains each line from the books.d file 
char *line_camel=(char*)malloc(sizeof(char) * MAXLINE); //allocating memory for the charecter pointers
char *title=(char*)malloc(sizeof(char) * MAXLINE);//allocating memory to hold the title from read from the client
char *search= (char*)malloc(sizeof(char) * MAXLINE);//allocating memory to store the search string 
char *temp=(char*)malloc(sizeof(char) * MAXLINE);
char authorname[MAXLINE]= "No Book\n";
authorname[7]='\n';
char *noresult="No Book\n"; //pointer to a charecter array
char semicolon=':';
int port; //used to store the port number
int listenfd; //listen fd for the socket address structure
int udpfd;
int connfd; //connfd
int maxfdp1;    
int nready;
socklen_t len;
fd_set rset;
FILE *fp; //File pointer 
int n;
int end;
pid_t childpid; //for fork system call
struct sockaddr_in servaddr, cliaddr; //declaring the socket address structure
if(argc != 2)// if the user has not given a port address through the parameters
{
    port = SERV_PORT; //assign the default port address
}
else
{
    port = atoi(argv[1]); //assign the user entered port number
}

listenfd = Socket(AF_INET, SOCK_STREAM, 0); //The server accepts the connections from the client
bzero(&servaddr, sizeof(servaddr));//clearing the contents of the socket address structure
servaddr.sin_family = AF_INET;//assigning IPV4 as the family for the socket address structure
servaddr.sin_port = htons(port); //converting from host to network order
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); //binding the socket address structure
Listen(listenfd, LISTENQ);//specifying the maximum backlog connections

udpfd = Socket(AF_INET, SOCK_DGRAM, 0);
bzero ( &servaddr, sizeof ( servaddr ) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl ( INADDR_ANY );
servaddr.sin_port = htons( port );

Bind(udpfd, (SA *) &servaddr, sizeof(servaddr) );

signal(SIGCHLD, sig_chld);
FD_ZERO ( &rset );
maxfdp1 = max ( listenfd, udpfd ) + 1;

fp = fopen("/home/631/common/books.d", "r");
while(true)
{
    FD_SET ( listenfd, &rset ); 
    FD_SET ( udpfd, &rset );

    if ( ( nready = select ( maxfdp1, &rset, 0, 0, 0 ) ) < 0 )
        continue;

    if ( FD_ISSET ( listenfd, &rset ) ) 
    {
        printf("Correctly reached the tcp part of the server\n");
        len = sizeof ( cliaddr ); 
        for(; ;)
        {
        errno = 0;
        connfd = Accept(listenfd, NULL, NULL);      
        if(errno == EINTR)
            continue;
        else
            break;
        }
        if ( ( childpid = Fork ( ) ) == 0 ) 
        {
                Close ( listenfd );
                while((n = Readline(connfd, title, MAXLINE)) > 0)
                {
                    title[n-1] = '\0';//explicitly putting a '\0' at the end of the input query to make it a  c-string
                    title=convert_tolower(title);//convert to lower case and catching it back
                    while( Fgets(line, MAXLINE, fp) != NULL )// reading each line from the server
                    {
                        string_copy(line_camel, line); //copying the line read to another line for result 
                        line=convert_tolower(line);//convert the line to lowercase
                        search= strstr(line, title); //checking if there is a substring with title (bookname) in the line
                        if( search != NULL && search[strlen(title)] == semicolon)
                        {                          
                        temp = strchr(line_camel, semicolon); //retrieving the substring from the earlier stored line 
                        end= strlen(temp); //always the last but one and the last by one plus one indices are '\0' and '\n'
                        temp[end-2] = '\n';// this place has : but we need to over write it with \n if we want to transfer
                        temp[end-2+1] ='\0';//mark the end of the string
                        ++temp;// *(temp) has : but we want to skip that charecter. if we dereference temp, we have ':'
                        string_copy(authorname, temp);//copying to author
                        //authorname[strlen(authorname)] = '\n'; //mark a '\n' charecter to transmit to the client
                        }
                    }      
                    end=strlen(authorname);
                    Writen(connfd, authorname, strlen(authorname));//wrting the saved authorname to the socket 
                    string_copy(authorname,noresult);//resetting it back 
                    authorname[7]='\n';
                    rewind(fp);//rewind the file pointer to the start of the file
                }       
                Close ( connfd ); 
                exit ( 0 );
        }
        Close ( connfd );
    }
    if ( FD_ISSET ( udpfd, &rset ) ) 
    {
        printf("Correctly reached the udp part of the server ");
        len = sizeof ( cliaddr );
        n = recvfrom ( udpfd, title , MAXLINE, 0, NULL, NULL);
        sendto( udpfd, title, strlen(title), 0, ( SA* ) &cliaddr, len );
        rewind(fp);//rewind the file pointer to the start of the file
    }
}
Close(listenfd);
Fclose(fp);
exit(0);
}
int main(int argc,char**argv)
{
char*line=(char*)malloc(sizeof(char)*MAXLINE);//行包含books.d文件中的每一行
char*line_camel=(char*)malloc(sizeof(char)*MAXLINE);//为charecter指针分配内存
char*title=(char*)malloc(sizeof(char)*MAXLINE);//分配内存保存从客户端读取的标题
char*search=(char*)malloc(sizeof(char)*MAXLINE);//分配内存来存储搜索字符串
char*temp=(char*)malloc(sizeof(char)*MAXLINE);
char authorname[MAXLINE]=“没有书\n”;
authorname[7]='\n';
char*noresult=“No Book\n”;//指向charecter数组的指针
字符分号=':';
int-port;//用于存储端口号
int listenfd;//侦听套接字地址结构的fd
int-udpfd;
int connfd;//connfd
int-maxfdp1;
国际研究所;
索克伦;
fd_集rset;
FILE*fp;//文件指针
int n;
内端;
pid\u t childpid;//用于fork系统调用
servaddr中的struct sockaddr\u,cliaddr;//声明套接字地址结构
if(argc!=2)//如果用户没有通过参数提供端口地址
{
port=SERV_port;//分配默认端口地址
}
其他的
{
port=atoi(argv[1]);//分配用户输入的端口号
}
listenfd=Socket(AF_INET,SOCK_STREAM,0);//服务器接受来自客户端的连接
bzero(&servaddr,sizeof(servaddr));//清除套接字地址结构的内容
servaddr.sin_family=AF_INET;//将IPV4指定为套接字地址结构的族
servaddr.sin_port=htons(port);//从主机到网络的转换顺序
servaddr.sin\u addr.s\u addr=htonl(INADDR\u ANY);
Bind(listenfd,(SA*)&servaddr,sizeof(servaddr));//绑定套接字地址结构
Listen(listenfd,LISTENQ);//指定最大积压连接数
udpfd=插座(AF INET,SOCK DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin\u addr.s\u addr=htonl(INADDR\u ANY);
servaddr.sinu端口=htons(端口);
绑定(udpfd,(SA*)和servaddr,sizeof(servaddr));
信号(SIGCHLD,sig_-chld);
FD_零和rset;
maxfdp1=max(listenfd,udpfd)+1;
fp=fopen(“/home/631/common/books.d”、“r”);
while(true)
{
FD_集合(列出的FD和rset);
FD_集(udpfd和rset);
如果((nready=select(maxfdp1,&rset,0,0))<0)
继续;
if(FD_ISSET(列出和重新设置))
{
printf(“已正确到达服务器的tcp部分\n”);
len=sizeof(cliaddr);
对于(;;)
{
errno=0;
connfd=Accept(listenfd,NULL,NULL);
如果(errno==EINTR)
继续;
其他的
打破
}
if((childpid=Fork())==0)
{
关闭(listenfd);
而((n=Readline(connfd,title,MAXLINE))>0)
{
title[n-1]='\0';//在输入查询的末尾显式添加'\0',使其成为c字符串
title=convert_tolower(title);//将其转换为小写并重新捕获
while(Fgets(line,MAXLINE,fp)!=NULL)//从服务器读取每一行
{
string_copy(line_camel,line);//将读取的行复制到另一行以获得结果
line=convert_tolower(line);//将行转换为小写
search=strstrstr(line,title);//检查行中是否有title(bookname)的子字符串
if(search!=NULL&&search[strlen(title)]==分号)
{                          
temp=strchr(第_行,分号);//从先前存储的行中检索子字符串
end=strlen(temp);//除了一之外的最后一个和由一加一的最后一个索引总是'\0'和'\n'
temp[end-2]='\n';//这个地方有:但是如果我们想传输,我们需要用\n重写它
temp[end-2+1]='\0';//标记字符串的结尾
++temp;//*(temp)有:但我们想跳过该字符。如果我们取消引用temp,我们有“:”
string_copy(authorname,temp);//复制到作者
//authorname[strlen(authorname)]='\n';//标记'\n'字符以传输到客户端
}
}      
end=strlen(authorname);
Writen(connfd,authorname,strlen(authorname));//将保存的authorname写入套接字
string_copy(authorname,noresult);//重新设置它
authorname[7]='\n';
倒带(fp);//倒带指向文件开头的文件指针
}       
关闭(connfd);
出口(0);
}
关闭(connfd);
}
if(FD_ISSET(udpfd和rset))
{
printf(“正确到达服务器的udp部分”);
len=sizeof(cliaddr);
n=recvfrom(udpfd,title,MAXLINE,0,NULL,NULL);
发送到(udpfd,标题,斯特伦(标题),0,(SA*)和cliaddr,len);
倒带(fp);//倒带指向文件开头的文件指针
}
}
关闭(listenfd);
Fclose(fp);
出口(0);
}

我试过你的代码。成功了。(当然,只有TCP/UDP部分。Rest被注释掉了):

科雷