- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下是server.c程序。当我将客户端连接到服务器时,服务器开始在接收消息部分无限循环
服务器.c
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
int main()
{
int sid,sid1,bin,lis,n,p,port, pid;
struct sockaddr_in sin,c;
socklen_t len;
char buffer[40]="";
char buffer1[20]="";
char ip[30]="192.168.65.241";
// printf("\nEnter the IP Address :");
// scanf("%s",ip);
printf("\nEnter the Port no.:");
scanf("%d",&port);
sid=socket(AF_INET,SOCK_STREAM,0);//END POINT CONN,
if(sid<0)
{
perror("Socket : ");
exit(0);
}
printf("\nSocket created successfully...");
sin.sin_family=AF_INET;
sin.sin_port=htons(port);
inet_aton(ip,&sin.sin_addr);//SERVER ADDRESS
bin=bind(sid,(struct sockaddr*)&sin,sizeof(sin));//BIND ADDRESS WITH SOCKET
if(bin<0)
{
perror("\nBind : ");
exit(0);
}
printf("\nBinding has been done successfully...");
lis=listen(sid,10);//LISTEN TO THE CLIENT
if(lis<0)
{
perror("\nListen : ");
exit(0);
}
len=sizeof(c);
while(1){
printf("A Client is connected to the server.....");
sid1=accept(sid,(struct sockaddr*)&sin,&len);//ACCEPT CONN.FROM CLIENT
if(sid1<1)
{
perror("\nAccept : ");
exit(0);
}
pid=fork();//CREATE PARENT AND CHILD PROCESS
if(pid<0){
perror("ERROR on fork");
exit(1);
}
if(pid==0){
lis=listen(sid,10);
printf("\nEnter message...(Type END to terminate):\n");
scanf("%s",buffer);
while(strcmp(buffer,"END")!=0)
{
send(sid,buffer,30,0);//SEND TO SERVER
scanf("%s",buffer);
}
send(sid,"END",5,0);
printf("\nTerminating the server...\n");
exit(0);
}
else{
n=recv(sid,buffer1,30,0);//RECEIVE FROM CLIENT
buffer[n]='\0';
n=strcmp(buffer1,"END");
while(strcmp(buffer1,"END")!=0)
{
**printf("\nClient: %s\n",buffer1);** // Infinite Looping
n=recv(sid,buffer1,30,0);
}
printf("\nA client has been disconnected from the server...\n");
exit(0);
}
close(sid); //CLOSE CONNECTIONS
close(sid1); //CLOSE CONNECTIONS
}
shutdown(sid,2); //SHUTDOWN READ AND WRITE
close(sid); //CLOSE CONNECTIONS
}
Client.c
客户端程序没有问题。添加了代码供您引用。
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
int main()
{
int sid,bi,con,n,p,port;
char buffer[50],ip[30];
sid=socket(AF_INET,SOCK_STREAM,0);//END POINT CONN.
if(sid==-1)
{
perror("\nSocket :");
exit(0);
}
printf("A Socket created successfully....");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
printf("\nEnter the IP Address :");
scanf("%s",ip);
printf("\nEnter the Port no. :");
scanf("%d",&port);
sin.sin_port = htons(port);
inet_aton(ip,&sin.sin_addr);
con=connect(sid,(struct sockaddr*)&sin,sizeof(sin));//CONNECT TO SERVER
if(con == -1)
{
perror("Connection : ");
exit(0);
}
printf("\nConnection is successfull to the server(%s)....\n",ip);
p=fork();
if(p)
{
printf("\nEnter message...(Type END to terminate):\n");
scanf("%s",buffer);
while(strcmp(buffer,"END")!=0)
{
send(sid,buffer,30,0);//SEND TO THE SERVER
scanf("%s",buffer);
}
send(sid,"END",4,0);
printf("\nTerminating the client...\n");
exit(0);
}
else
{
n=recv(sid,buffer,30,0);//RECV.FROM SERVER
printf("%d",n);
buffer[n+1]='\0';
while(strcmp(buffer,"END")!=0)
{
printf("\nServer: %s\n",buffer);
n=recv(sid,buffer,30,0);
}
send(sid,"END",4,0);
exit(0);
}
printf("\nClient is terminated...\n");
shutdown(sid,2);//SHUTDOWN READ AND WRITE
close(sid); //CLOSE CONNECTIONS
}
请尽快让我知道解决方案。
最佳答案
主要问题,即无限循环,只是因为 recv()
是在服务器的主套接字 (sid
) 上调用的,而不是 返回的套接字>accept()
- sid1
。更改此:
n=recv(sid,buffer1,30,0);//RECEIVE FROM CLIENT
至
n=recv(sid1,buffer1,30,0);//RECEIVE FROM CLIENT
和
n=recv(sid,buffer1,30,0);
至
n=recv(sid1,buffer1,30,0);
这样你就可以开展业务了。
服务器代码还存在其他问题:
recv()
被告知将最多 30 个字节读入 buffer1
,然而,buffer1
的大小只有 20 个字节 - 你有一个缓冲区那里已经超支了。recv()
返回 0 表示。listen()
在第 64 行的服务器中被不必要地调用。上面的列表
可能
并不详尽。
关于c - 套接字编程 TCP 。服务器编程不断循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29008526/
我是 ZMQ 的新手。我发现 ZMQ 套接字实现比 winsock 简单得多。但我怀疑 “使用 ZMQ TCP 套接字创建的客户端可以与传统的 TCP 服务器通信吗?” 换句话说我的 ZMQ 客户端可
我想使用 TCP 协议(protocol) 将数据发送到 Logstash。为了发送数据,我正在使用 Node-RED。一个简单的配置如下所示: 在 Logstash 文件夹中,我创建了一个名为 no
当我尝试更改窗口缩放选项时,作为 root,我可以通过在 /proc/sys/net/中执行 net.ipv4.tcp_mem=16777000 来更改值。如果我必须更改这 100 个系统,那将需要大
明天做一些练习题,这道做不出来 TCP 服务器连接 TCP 客户端进行通信所需的最小套接字端口数是多少? 肯定只有两个吧?一个用于服务器,一个用于客户端,但这似乎是显而易见的。我的伙伴们认为 TCP
考虑一个存在一个服务器和多个客户端的场景。每个客户端创建 TCP 连接以与服务器交互。 TCP alive的三种用法: 服务器端保活:服务器发送 TCP 保活以确保客户端处于事件状态。如果客户端死了,
TCP TAHOE 和 TCP RENO 有什么区别。 我想知道的是关于 3-dup-ack 和超时的行为? SST 发生了什么变化? 谢谢! 最佳答案 TCP Tahoe 和 Reno 是处理 TC
大家早上好。我一直在阅读(其中大部分在堆栈溢出中)关于如何进行安全密码身份验证(散列 n 次,使用盐等)但我怀疑我将如何在我的 TCP 客户端中实际实现它-服务器架构。 我已经实现并测试了我需要的方法
在遍历 RFC793 时,我开始知道应该以这种方式选择初始序列号段重叠被阻止。 有人能解释一下如果发生重叠,重复段将如何影响 TCP? 最佳答案 不同的操作系统有不同的行为。参见 http://ins
你能举例说明一下tcp/ip中nagle算法的概念吗? 最佳答案 我认为Wikipedia在开头的段落中做得很好。 Nagle's document, Congestion Control in IP
似乎最大 TCP 接收窗口大小为 1GB(使用缩放时)。因此,仍然可以用一个连接填充 100Gb 管道的最大 RTT 是 40ms(因为 2 * 40E-3 * 100E9/8 = 1GB)。这会将这
考虑在两个 TCP 端点之间建立的 TCP 连接,其中一个调用: 关闭():此处,不允许进一步读取或写入。 关机(fd,SHUT_WR):这会将全双工连接转换为单工连接,其中调用 SHUT_WR 的端
我是在 Lua 中编写解析器的新手,我有两个简短的问题。我有一个包含 TCP 选项的数据包,如 MSS、TCP SACK、时间戳、NOP、窗口比例、未知。我基本上是在尝试剖析 TCP 选项字段中的未知
TCP 是否不负责通过在传输过程中发生丢失等情况时采取任何可能必要的措施来确保通过网络完整地发送流? 它做的不对吗? 为什么更高的应用层协议(protocol)及其应用程序仍然执行校验和? 最佳答案
考虑使用 10 Mbps 链路的单个 TCP (Reno) 连接。假设此链路不缓冲数据并且接收方的接收缓冲区比拥塞窗口大得多。设每个 TCP 段的大小为 1500 字节,发送方和接收方之间连接的双向传
考虑这样一个场景,有client-a和server-b。 server-b 禁用了 TCP keepalive。 server-b 没有任何应用程序逻辑来检查 TCP 连接是否打开。 client-a
我正在尝试用 Rust 编写回显服务器。 use std::net::{TcpStream, TcpListener}; use std::io::prelude::*; fn main() {
听说对于TCP连接,服务器会监听一个端口,并使用另一个端口发送数据。 例如,Web 服务器监听端口 80。每当客户端连接到它时,该服务器将使用另一个端口(比如 9999)向客户端发送数据(Web 内容
我试图了解带有标记 PSH 和标记 URG 的 TCP 段之间的区别。我阅读了 RFC,但仍然无法理解,其中一个在将数据发送到进程之前缓冲数据而另一个没有吗? 最佳答案 它们是两种截然不同的机制。 #
有第三方服务公开 TCP 服务器,我的 Node 服务器(TCP 客户端)应使用 tls Node 模块与其建立 TCP 连接。作为 TCP 客户端, Node 服务器同时也是 HTTP 服务器,它应
我正在发送一些 TCP SYN 数据包以获得 TCP RST 的返回。为了识别每个探测器,我在 TCP 序列字段中包含一个计数器。我注意到以下几点: 当SYN probe中的sequence numb
我是一名优秀的程序员,十分优秀!