gpt4 book ai didi

c - ws ://in a C program

转载 作者:行者123 更新时间:2023-12-01 13:48:03 25 4
gpt4 key购买 nike

我有一个程序在套接字上作为监听器运行。从一个 javascript 页面,我可以使用 Websocket (ws://) 发送一个打开的套接字请求。我的问题在于我似乎无法从 C 程序中看到握手或回复它。我使用 wireshark 来确认 javascript 正在发送正确的握手,但是当我尝试在缓冲区中接收它时,我得到的都是零。

此外,我能够在 ws://echo.websocket.org 上测试 javascript,所以我很确定它工作正常。我从 Web 浏览器的控制台收到的错误表明没有收到对握手的回复。

编辑:我知道这是因为我没有设置回复。

更新的C程序如下

#include <sys/socket.h> //for socket
#include <netinet/in.h>
#include <arpa/inet.h> //inet_addr
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h> //strlen
#include <sys/types.h>

/* main function */
int main() {
int listenfd = 0, connfd = 0;
int status;
struct sockaddr_in serv_addr;
char revcBuff[1025];
int dataPresent = 0;
memset(&serv_addr, 0, sizeof(serv_addr)); //clear the memory for the server_addr
memset(&revcBuff, 0, sizeof(revcBuff)); //clear the memory for the buffer
serv_addr.sin_family = AF_INET; //specifiys that the address is IPv4
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);//gets the local address
//serv_addr.sin_port = htons(13669); //gets the local port
serv_addr.sin_port = htons(5000); //gets the local port

//designate the socket
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(listenfd < 0) {
fprintf(stderr, "Socket creation failed due to: %s\n", strerror( errno )); //If the socket creation fails get the error
return EXIT_FAILURE;
}

//binds to the local address and port to listen
while ( (bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)))< 0) {
fprintf(stderr, "bind failed due to: %s\n Retrying.....\n", strerror( errno )); //If the bind fails get the error
sleep(3); //time in seconds till next try
}

//listen on socket
if ( (listen(listenfd, 10))< 0) {
fprintf(stderr, "listen failed due to: %s\n", strerror( errno ));//If the listen fails get the error
return EXIT_FAILURE;
}

//main program loop
fprintf(stderr, "serverSock0-14 now running in listen mode \n");
while (connfd >= 0) { //while socket is not in error mode try to accept incoming socket requests
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); //wait for an incoming socket request
if ( connfd == -1) { //if atempted socket request failed
fprintf(stderr, "accept failed due to: %s\n", strerror( errno )); // if the accept fails
}
fprintf(stdout, "connection opened \n");
while (dataPresent != -1) { //keep socket open till you receive a packet
dataPresent = recv(connfd, &revcBuff, sizeof revcBuff, MSG_TRUNC); //receive data from buffer
//dataPresent = recv(connfd, revcBuff, sizeof revcBuff, MSG_DONTWAIT); //receive data from buffer
if ( dataPresent == -1) { //test if new data is bad
fprintf(stderr, "Receive failed due to: %s\n", strerror( errno )); // if the accept fails
} else if ( dataPresent == 0) { // if the socket is closed on the other end
break; // Escape to the close socket statement
} else {
revcBuff[dataPresent] = '\0'; // null terminate the buffer
fprintf(stdout, "Buff Hex unsigned: %x %x %x \n", (unsigned int) revcBuff[0], (unsigned int) revcBuff[1], (unsigned int) revcBuff[2]);
fprintf(stdout, "Buff Hex: %x %x %x %x %x \n", revcBuff[0], revcBuff[1], revcBuff[2], revcBuff[3], revcBuff[4], revcBuff[5]);
fprintf(stdout, "Buff: %s \nbytes received: %i \n", revcBuff, dataPresent); //Print out what you received
}
dataPresent = 0;
usleep(7000);
}
if ((close(connfd))==0){//close the socket
fprintf(stdout, "connection closed\n");
}
} //end main program loop
return EXIT_SUCCESS;
}

任何方向都会非常有帮助。我对 C 的世界还是很陌生。

最佳答案

首先,正如其他人所指出的,您应该在 recv() 调用中使用 connfd:

recv(connfd, (char*)sendBuff, sizeof sendBuff, MSG_DONTWAIT);

如果您有可用的 telnet 客户端可以连接到您的程序,您应该使用它来试验您的代码。例如,如果您在 Linux 上运行,请在一个终端窗口中启动您的程序,然后在另一个终端中键入

telnet localhost 5000

这是我这样做时发生的情况:

## The terminal where I run the program (a.out):
$ ./a.out
connection opened
Buff:
connection closed
connection opened
Buff:
connection closed

## The terminal window I connect from:

$ telnet localhost 5000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
$ telnet localhost 5000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

看看发生了什么?这并不奇怪:代码接受来自客户端的连接,尝试从客户端读取消息而不给它发送消息的机会,什么也得不到(当然!),永远不会将任何东西发送回客户端, 并关闭连接。然后它等待下一个连接。

请阅读一些关于 C 套接字编程的教程,使用它,让它与 telnet 客户端一起工作,然后转到 websockets。

无论如何,这是一个好的开始。祝你好运!

关于c - ws ://in a C program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34322736/

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