gpt4 book ai didi

c - g_io_channel + socket = server ,仍然没有收到正确的数据,只得到一个客户端?用C语言

转载 作者:行者123 更新时间:2023-11-30 16:02:32 27 4
gpt4 key购买 nike

各位,

如果您不介意,请参阅以下代码:

#include <glib.h>
#include <gio/gio.h> // gio channel

#include <sys/socket.h> //socket();
#include <netdb.h> // structure

#include <stdio.h> // printf

void deal(GIOChannel *in, GIOCondition condition, gpointer data)
{
struct sockaddr_storage income;

int insock = g_io_channel_unix_get_fd(in);
socklen_t income_len = sizeof(income);
int newsock = accept(insock, (struct sockaddr*)&income, &income_len );
if(newsock == -1)
{
printf("failure on newsock\n");
}

char buff[128];

int recv_total = 0;
int recv_byte = 128;
int recv_sizing;

while (recv_total < recv_byte ){

recv_sizing = recv(newsock,buff + recv_total,recv_byte,0);

// breaking if recv_sizing = -1 assuming as error, 0 assuming as lost communication from client suddenly
if(recv_sizing < 0 || recv_sizing == 0)
{
printf("connection lost or error while recv(); [ just guess ] number : %d \n",recv_sizing);
break;
}

recv_byte -= recv_sizing;
recv_total += recv_sizing;


}


buff[recv_total] = '\0';
//recv_sizing = recv(newsock,buff,recv_byte,0);
printf("data : %s\n",buff);

close(newsock); // close immediate and look for another some1 new


}

int main()
{
GIOChannel *in;


struct sockaddr_in my;
my.sin_addr.s_addr = INADDR_ANY;
my.sin_family = AF_INET;
my.sin_port = htons(3000);

//socket initiate root socket
int rsock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

//allow re-use address
setsockopt(rsock,SOL_SOCKET,SO_REUSEADDR,(int*)1,sizeof(int));

//binding
bind(rsock,(struct sockaddr*)&my,sizeof(my));

//listen
listen(rsock,10);


in = g_io_channel_unix_new(rsock);

g_io_add_watch(in, G_IO_IN | G_IO_OUT | G_IO_HUP, (GIOFunc) deal, NULL);


GMainLoop *loop = g_main_loop_new(NULL,FALSE); // pengganti while(1) ato gtk_main_loop
g_main_loop_run(loop);
return 0;
}

它被编译了:

$ gcc -o dengersocket_glib dengersocket_glib.c `pkg-config --cflags --libs glib-2.0`

现在监听并期待来自客户端的任何数据包数据客户端发送以下数据包:

$ echo wew | nc -v localhost 3000 
nc: connect to localhost port 3000 (tcp) failed: Connection refused
Connection to localhost 3000 port [tcp/*] succeeded!

现在服务器收到以下奇怪的数据包:

$ ./dengersocket_glib 
data : �o=

我的问题是,我的代码哪里出了问题?,

1.如何获得正确的数据包并且每个客户端都可以连接到服务器? [已解决]

2.传递数据解决了,但还是只能接受一个客户端,如何获取多个客户端?

最佳答案

int recv_total;

应该是

int recv_total = 0;

由于 recv_total 由于缺乏初始化而具有随机垃圾值,您还将在 buf 中获得随机垃圾数据,除非 recv_total只是碰巧是<128,并且缓冲区中的第一个字符将是垃圾,除非recv_total碰巧是0。

编辑:另外,您的接受调用是错误的,您将大小转换为 void * 但应该传递一个指向 socklen_t 的指针,该指针应包含并接收 sockaddr 的大小。

socklen_t ss = sizeof(income);
accept(..., &ss);

然后,检查accept的返回值,看看你是否得到了一个有效的套接字。

if (newsock == -1) {
printf("...");
}

关于c - g_io_channel + socket = server ,仍然没有收到正确的数据,只得到一个客户端?用C语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5206212/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com