C Socket Send() 在程序终止之前不会发送
这是我的第一个问题,所以我将不胜感激。我正在尝试学习 Unix 的 C 套接字。作为学习的一部分,我尝试编写一个简单的类似 telnet 的客户端,该客户端连接到指定端口上的主机,打印从服务器接收到的任何字符,并将用户写入的任何内容发送到控制台。它接收良好,但是当我尝试发送字符串时,没有任何反应。然后,当我中断程序时,我尝试发送的所有字符串都会被发送。提前致谢。我可能只是很愚蠢。
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
int SetupSock(char *host, char *port) {
int s, status;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(host, port, &hints, &res);
if((s = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
printf("Could not create sock...root?\n");
exit(1);
}
if((status = connect(s, res->ai_addr, res->ai_addrlen)) == -1) {
printf("Connect Failed: %s\n", strerror(errno));
printf("%s", strerror( errno ));
printf("\n");
exit(1);
}
return s;
}
int main (void) {
char *host = malloc(100), *port = malloc(20), buf[2], *msg = malloc(1000);
struct timeval waitid;
fd_set read_flags, write_flags;
signal(SIGPIPE, SIG_IGN);
int sock, flags;
//Input Host and Port
printf("Host: ");
gets(host);
printf("Port: ");
gets(port);
sock = SetupSock(host, port);
flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
FD_ZERO(&read_flags);
FD_ZERO(&write_flags);
FD_SET(sock, &read_flags);
FD_SET(fileno(stdin), &read_flags);
fflush(0);
int pid = fork();
if (pid == 0) {
int status;
close(stdout);
while(1) {
select(sock + 1, &read_flags, &write_flags, (fd_set*) 0, &waitid);
if (FD_ISSET(fileno(stdin), &read_flags)) {
gets(msg);
if((status = send(sock, msg, strlen(msg), MSG_NOSIGNAL)) == -1) {
printf("Send Failed: %s\n", strerror(errno));
}
}
}
}
else {
close(stdin);
while(1) {
select(sock + 1, &read_flags, &write_flags, (fd_set*) 0, &waitid);
if (FD_ISSET(sock, &read_flags)) {
if(recv(sock, &buf, 1, 0) > 0){
printf("%s", buf);
}
}
}
}
close(sock);
return 0;
}
This is my first question, so your grace would be appreciated. I'm trying to learn C Sockets for Unix. As part of my learning, I tried to code a simple telnet-like client that connects to a host on a specified port, prints any characters received from the server, and sends anything the user writes to the console. It receives fine, but when I try to send a string, nothing happens. Then, when I interrupt the program, all the strings I tried to send get sent. Thanks in advanced. I'm probably just being stupid.
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
int SetupSock(char *host, char *port) {
int s, status;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(host, port, &hints, &res);
if((s = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
printf("Could not create sock...root?\n");
exit(1);
}
if((status = connect(s, res->ai_addr, res->ai_addrlen)) == -1) {
printf("Connect Failed: %s\n", strerror(errno));
printf("%s", strerror( errno ));
printf("\n");
exit(1);
}
return s;
}
int main (void) {
char *host = malloc(100), *port = malloc(20), buf[2], *msg = malloc(1000);
struct timeval waitid;
fd_set read_flags, write_flags;
signal(SIGPIPE, SIG_IGN);
int sock, flags;
//Input Host and Port
printf("Host: ");
gets(host);
printf("Port: ");
gets(port);
sock = SetupSock(host, port);
flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
FD_ZERO(&read_flags);
FD_ZERO(&write_flags);
FD_SET(sock, &read_flags);
FD_SET(fileno(stdin), &read_flags);
fflush(0);
int pid = fork();
if (pid == 0) {
int status;
close(stdout);
while(1) {
select(sock + 1, &read_flags, &write_flags, (fd_set*) 0, &waitid);
if (FD_ISSET(fileno(stdin), &read_flags)) {
gets(msg);
if((status = send(sock, msg, strlen(msg), MSG_NOSIGNAL)) == -1) {
printf("Send Failed: %s\n", strerror(errno));
}
}
}
}
else {
close(stdin);
while(1) {
select(sock + 1, &read_flags, &write_flags, (fd_set*) 0, &waitid);
if (FD_ISSET(sock, &read_flags)) {
if(recv(sock, &buf, 1, 0) > 0){
printf("%s", buf);
}
}
}
}
close(sock);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
select
在这里仅执行一次。它应该在一个循环内。select
只是等待提供的文件描述符集上发生事件。处理每个事件后应再次调用它。
Your
select
is performed only once here. It should be inside a loop.select
just waits for an event to occur on the provided set of file descriptors.It should be called again after each event has been processed.
为了覆盖其他答案错过的区域:
select
和 stdio 不要混合。由于存在缓冲,无法通过fileno(stdin)
是否可读来准确判断stdin
是否可读。To cover an area the other answer missed:
select
and stdio do not mix. You cannot get an accurate result for whetherstdin
is readable by whetherfileno(stdin)
is readable due to buffering.