Reputation: 2255
So I've been following Beej's guide to try to learn some basic networking-http://beej.us/guide/bgnet/
So I decided to play around with some of the code that was written for some practice- so I'm trying to make a simple terminal chat room where an input IP address allows a user to connect- the trouble I'm having is that on this "server" file I've edited, it works well when it sends information, but the recv function call freezes up terminal for some reason and I'm not really sure why. I assume that when accept works correctly, the new_fd socket can be used to send and receive from, as it represents the client's socket...This is where I am sort of stuck.
Also, I was wondering what other libraries I should learn to get more practical networking experience. I feel like this is a good way to get my feet wet with really low level stuff (I like it because it's pretty deep), but I don't really see the C networking library as particularly effective in comparison to other things that are out there-
int waiting = 1;
char input = ' ';
char message[100];
memset(&message, 0, sizeof message);
while(waiting) { // main accept() loop
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);//converts an IP address we get signal from to readable string
printf("%s wants to chat with you.\nAccept? Y/n\n", s);
scanf("%c", &input);
if (input == 'Y' || input == 'y') {
int rv;
/*if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("client: connect");
continue;
}*/
waiting = 0;
}
else {
printf("Waiting Mode\n");
}
}
char buf[100];
char prev[100];
char nil[100];
memset(nil, 0, sizeof nil);
int numbytes;
while (1) {
fgets(message, 100, stdin);
if (send(new_fd, message, 100, 0) == -1) {
perror("send");
}
if ((numbytes = recv(new_fd, buf, 100, 0)) == -1) {
perror("recv");
exit(1);
}
/*if (strcmp(prev, buf) == 0 || strcmp(nil, buf) == 0 ) {
continue;
}
buf[numbytes] = '\0';
printf("\n%s\n", buf);
memmove(prev, buf, sizeof buf);*/
}
return 0;
}
Upvotes: 0
Views: 1790
Reputation: 84642
As has been mentioned above in the comments, your terminal "freezing" is due to your call Blocking on recv
. Unless another member of the chat-room sends additional information, your program will simply sit there and wait forever. The guide you mention is a very good reference for network programming. Go back and read Section 7.1 and 7.2 again on blocking and in particular his use of select() in the cheezy-chat-room example. They are what allow the example to work without getting stuck.
If you are not familiar with select()
, pselect()
, and poll()
, don't get frustrated, they are some of the most important, but most challenging aspect of C, to make friends with. Take a little time to review the section 7.1 and 7.2 of the guide and spend a little time understanding select() and that will take care of your problem. select()
, pselect()
, and poll()
are not limited in usefulness to networking, they are important in all aspects of program IO.
Upvotes: 2