Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encountered with problem when Porting UDP socket communication c code using emscripten #5196

Closed
Cooliodtryl opened this issue May 4, 2017 · 5 comments

Comments

@Cooliodtryl
Copy link

Cooliodtryl commented May 4, 2017

I write simple UDP communicate code using C,just as follows:
Client.c:


1. #include <stdio.h>
2. #include <string.h>
3. #include <unistd.h>
4. #include <sys/types.h>
5. #include <sys/socket.h>
6. #include <netinet/in.h>
7. #include <arpa/inet.h>
8. #include <errno.h>
9. #include <stdlib.h>
10. 
11. #define MAX_LINE 80
12. 
13. int main(int argc, char *argv[ ])
14. {
15. 	struct sockaddr_in sin;
16.     	struct sockaddr_in cin;
17. 	int port = 40000;
18. 	socklen_t addr_len;
19. 	int s_fd;
20. 	char *str = "test"; 
21.     	char buf[MAX_LINE];
22.     	char add_p[INET_ADDRSTRLEN];
23.     	int n;
24.     
25. 	if(argc != 2){
26. 		printf("wrong command\n");
27. 		exit(1);
28. 	}
29. 	str = argv[1];
30. 	
31. 	bzero(&sin, sizeof(sin));
32. 	sin.sin_family = AF_INET; 
33. 	inet_pton(AF_INET, "192.168.8.177", &sin.sin_addr); 
34. // 	inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr); 
35. 	sin.sin_port = htons(port);
36.     
37. 	s_fd = socket(AF_INET, SOCK_DGRAM, 0);
38. 	if(s_fd == -1){
39.      	perror("fail to create socket");
40. 		exit(1);
41. 	}
42. 	
43.     	n = sendto(s_fd, str, strlen(str) + 1, 0, (struct sockaddr *) &sin, sizeof(sin));
44. 	if(n == -1){
45.      	perror("fail to send\n");
46. 		exit(1);
47. 	}
48.     
49. 	addr_len = sizeof(cin);
50. 	n = recvfrom(s_fd, buf, MAX_LINE, 0, (struct sockaddr *) &cin, &addr_len);
51. 	if(n == -1){
52.     		perror("fail to receive\n");
53.     		exit(1);
54. 	}else
55. 		printf("recive from server: %s\n", buf); 
56. 	
57. 	if(close(s_fd) == -1){ 
58. 		perror("fail to close.\n");
59. 		exit(1);
60. 	}
61. 
62. 	return 0;
63. }

Server.c:


1. #include <stdio.h>
2. #include <string.h>
3. #include <unistd.h>
4. #include <ctype.h>
5. #include <sys/socket.h>
6. #include <netinet/in.h>
7. #include <arpa/inet.h>
8. #include <stdlib.h>
9. #include <errno.h>
10. 
11. #define MAX_LINE 100
12. 
13. void my_fun(char * p)
14. {
15. 	if(p == NULL) /* ¿ÕŽ® */
16. 		return;
17. 
18. 	for (; *p != '\0'; p++)
19. 		if(*p >= 'A'&& *p <= 'Z')
20. 			*p = *p -'A'+ 'a';
21. }
22. 
23. int main(void)
24. {
25.     	struct sockaddr_in sin;
26.     	struct sockaddr_in cin;
27.     	int s_fd;
28. 	int port = 30000; 
29. 	socklen_t addr_len;
30.     	char buf[MAX_LINE];
31.     	char addr_p[INET_ADDRSTRLEN]; 
32.     	int n;
33.      
34.     	bzero(&sin, sizeof(sin));
35.     	sin.sin_family = AF_INET;
36.     	sin.sin_addr.s_addr = INADDR_ANY;
37.     	
38.     
39.      	s_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
40.      	if (s_fd == -1) {
41.      	perror("fail to create socket");
42.          	exit(1);
43.      		}
44.      	int reuse = 1;
45.         if (setsockopt(s_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
46.         {
47.             perror("Set udpsocket reuse error");
48.             exit(2);
49.         }
50. // 	for(;port<40010;port++)
51. // 	{
52. 	    sin.sin_port = htons(port);
53. 	    if(bind(s_fd, (struct sockaddr *) &sin, sizeof(sin)) == -1){ 
54. 		  perror("call to bind");
55. 		  exit(1);
56. 	    }else{
57. 		  printf("bind [%d] success!\n",port);
58. 	    }
59. 		  
60. // 	}
61.      	while (1) {
62.          	addr_len = sizeof(sin);
63. 
64.          	n = recvfrom(s_fd, buf, MAX_LINE, 0, (struct sockaddr *) &cin, &addr_len);
65.          	if (n == -1) {
66. 			perror("fail to receive\n");
67.               exit(1);
68.          	}
69. 		
70.         inet_ntop(AF_INET, &cin.sin_addr, addr_p, sizeof(addr_p)); 
71.          
72. 		printf("client IP is %s, port is %d\n", addr_p, ntohs(cin.sin_port));
73.     		printf("content is : %s\n", buf); 
74.          
75. 		my_fun(buf); 
76. 		
77. 		n = sendto(s_fd, buf, n, 0, (struct sockaddr *) &cin, addr_len);
78. 		if (n == -1){
79. 			perror("fail to send\n");
80.               exit(1);
81.          }
82.      }
83. 
84. 	if(close(s_fd) == -1){
85. 		perror("fail to close");
86. 		exit(1);
87. 	}
88. 
89. 	return 0;
90. }

now:
compile server:
gcc server.c -o server
then compile client using emcc:
emcc client.c -o client.html
all runs ok,I run server firstly in one linux terminal,and open client.html using Chrome,but I got wrong message:
WebSocket connection to 'ws://172.18.193.73:30000/' failed: WebSocket is closed before the connection is established.
Obviously,emscripten converts udp socket in c language to websocket in javascript,but websocket bases on tcp ,not udp, So the problem is how can I port udp socket syscall to javascript using emscripten?welcome you to talk over with me!

@juj juj added the networking label May 5, 2017
@juj
Copy link
Collaborator

juj commented May 5, 2017

It is unfortunately not possible to send UDP datagrams from web browsers. The BSD sockets code fakes the API and makes the socket attempt to connect to a websockets server, so your listening endpoint will need to be a websockets communicating server. There exists projects such as websockify (https://github.com/novnc/websockify), which enable browsers to connect to arbitrary TCP servers.

If you need UDP style communication, then it is possible to utilize WebRTC data channels, which gives unreliable and unordered transmission. However that is also connectionful, and the browser must connect to a WebRTC server and cannot talk to an arbitrary UDP listening socket.

Do you need to connect to an UDP server, or can you migrate to a Websocket or WebRTC data channels server? If UDP server is really required, check out something like https://dimitrisfousteris.wordpress.com/2014/12/07/websocket-udp-proxy-with-node-js/ which is like websockify but for UDP.

@Cooliodtryl
Copy link
Author

Thanks to you precious suggestions,why I need to connect browser to UDP endpoint?I want to create p2p videostream channal between the webbrowser and our device,so the plan which converts websocket stream and UDP stream is not possible,because compared with this plan,I have better choice to connect webbrowser and devices using TCP channel ( and websocket),between both methods need to connect transserver and device!so in a few words,since browser doesn't support UDP natively,to create p2p stream channel between webbrowser and device,the only choice is to use webrtc,do you agree with me?

@juj
Copy link
Collaborator

juj commented May 8, 2017

That makes sense. I have not used WebRTC for video, though perhaps https://www.tutorialspoint.com/webrtc/webrtc_video_demo.htm and https://www.html5rocks.com/en/tutorials/webrtc/basics/ can help towards an implementation here?

@mittorn
Copy link

mittorn commented Aug 22, 2017

Try this:
FWGS@efcb8ec
https://github.com/mittorn/websockify-c/tree/udp
It is working as complete proxy for both tcp and udp now.

@stale
Copy link

stale bot commented Aug 30, 2019

This issue has been automatically marked as stale because there has been no activity in the past 2 years. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.

@stale stale bot added the wontfix label Aug 30, 2019
@stale stale bot closed this as completed Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants