-
Send/Recieve problem
I need some assistance. The server part of my program is not recieving the screen name that the client should be sending. I will include both the client and server source. The error in the client should be the send before the main loop and in the server it is the recv in first main block of code in the main loop.
-
-
-
>>cin.getline(buf, 1, '\n');
Why limit the size to 1? That only leaves room for the \0, no actual data, making this a little pointless:
>>len = strlen(buf);
-
Ohh haha. I had made it like that to test something earlier and forgot to change it back. I still doesn't work though.
-
-
Your server is doing wrong here:
>>if (recv(sock, name, MAX, 0) == -1)
It should be :
>>if (recv(newfd, name, MAX, 0) == -1)
There might be other places where you've used "sock" when you should've used "newfd", so I'd check through your code, if I were you. :)
Agghhh, wait a minute !! What's this:
>>char *buf, *name, *userName, *ann;
buf and name are only a pointers? But you used name to write data to (from recv), where is that pointer pointing to ?? ;)
-
Hehe. Both of those I caught and I believe updated them with the new client. When the screen name is sent to the server, it is now getting to the line what it shows their screen name but instead of showing the screen name is prints (null) on the server and the client seg faults due to an error in the server.
-
-
I want to do a very simple network thing where it passes a character array with 5 characters (6 with the null terminator)...
both clients need to act like servers as well... I want it to be that when one person types in the 5 characters, it sends it out to the other... and vice versa.
I only want two people (I'm fine with having to do IP stuff)... how do you do this? Do I need as much code as he had?
-
-
@Clown: This:
buf = name = userName = ann = NULL;
doesn't solve your problem. You need to point the pointers at some valid memory where they can store data. Either that, or make them arrays instead:
>>char name[LENGTH];
-
You won't need as much. You would probably only need one file to do it. You would just need to set up a menu at start up where the user can make a choice to become the server or client. If he wants to be a server then bind and listen else then he wants to be a client and then just connect to the server NOTE: the server would already need to be up to connect to. Then you could either user a while loop of send and recv or use select. I have found something that should help you with what you are doing.
Alternating Chat
-
Good ole Computer Science teacher was wrong. He said it would work but let's see if it solves the problem. One question that has been bothering me. The recv function, does it sit on the socket and wait for data to be sent or does it check the connection for data to be read at the time of calling and if there is no data to read it quits?
-
>>The recv function, does it sit on the socket and wait for data
You need to research "blocking" and "non-blocking".
A blocking socket will wait for data, and your program will hang until it gets some.
A non-blocking socket will cause recv() to terminate early if there's no data (with a specific errno value).
There is some examples in here and in Beej's guide (the link I posted for Trauts.