Need help with send() & recv(), Win Socketing.

Hey, I just recently got into Win Socketing and I have a chat server/client setup up. And it works, but it only sends the first word of a message (The message has to be a character) so I was wondering one of two things: 1) Has anyone found a way to pass a string through send()/recv().
2) Does anyone know how I can convert a std::string to a char array. I have followed many tutorials a they don't work.

So if any code is need to see, or if your just interested in making a client/server program yourself, I have np sharing my code ;).

Thanks in advance!
Hey I figured this out, I just had to use cin.getline(); instead of cin >> var;
but now I need to figure out how I can pass cin.getline() more then once on the same var. Any help appreciated. :)
Well, a bit of code should be shown.

But, given:

mySocket is your SOCKET
myString is your std::string
myFlags are your chosen flags (usually 0)

this should be the way:

 
send( mySocket, myString.c_str(), myString.length(), myFlags );


And to receive it:

1
2
3
char myBuffer[512] = {};
recv( mySocket, myBuffer, 512, myFlags );
std::string myString = myBuffer;


The limitation is, as you can see, 512 bytes for a single "packet of data".
To get rid of it, you MUST be connected through TCP, and you should change the code.

But I do not suggest this, this can become a big security issue, because someone else may send a very very big message, trying to crash your program by forcing it to request a lot of RAM memory.

But, if you want me to show you anyways, I can show.

Also, both send and recv return these values:

1. Positive integer:
The size of the sent/received data
2. 0:
The connection has been closed (Only for recv)
3. SOCKET_ERROR:
Connection error
Last edited on
Thank you so much, I should have know this would work but i haven't slept in two days, just moved and im still trying to get settled -.- but thank you again. I like this much more then the cin.getline(). If you have an answer to my other question that would be great too ;). but as for the "packet limitation" I was planning on what you said and if they need they could simply send a second message, like most programs do (i.e Skype, I think TeamViewer)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while (!CloseWin)
		{
			if (Update)
			{
			DisMsg = RevMsg;
			system("Cls");
			cout << "-----------------------------" << endl;
			cout << "         Lets Chat!!!" << endl;
			cout << "-----------------------------" << endl << endl;
			Update = false;
			}

			answer = recv(sConnect, RevMsg, sizeof(RevMsg), NULL);
			if (DisMsg != RevMsg)
			{
			DisMsg = RevMsg;
			cout << DisMsg << endl;
			}
			
			getline(cin, SndMsg);
			answer = send(sConnect, SndMsg.c_str(), sizeof(SndMsg), NULL);
		}


This mostly works the problem here is that I can only send one message then it doesn't pause for the player to enter the message at getline(cin, SndMsg); and one person must wait for the other to reply before they can. How can I make this more like other IM programs where you can type at anytime?

EDIT:
Lol I just realized something, I am not actually going to limit message sizes, because this is going to be a downloadable package where people will be able to download the server & client then customize the server options to be account password protected and the title ect. then distribute the client as needed. This will be for small org. or communities. They will obviously need to portforward to use it tho, I might set up my own server so others can talk about the product, but i see no other use for it, for me. Yet.
Last edited on
Well, for HTML you should use another Web-Server like Lighttpd or Apache.
Trust me, there WILL be many, many security issues if you try to build it on your own, important http daemons (http servers) DO have these issues and are resolving them slowly, update after update (Now, there are very few issues, i'm sure).
Actually, my solution ended up being Lighttpd, so I can help you set it up.

Also, redistributing Client & Server does not mean you are forced to abide by the HTML rules while you build your program.
You could download the programs via a web browser but still they can send fixed-size data.

Or, you could do as most people do:

Organize your packet like this:

1. The first two bytes could store the "packet ID".
Like, "This packet says your nickname is X" or "This packet says you should receive file X, part Y".

2. The next four bytes, in your case, could store the full size of the data.
3. The next four bytes, could store the data in the current packet
4. The next bytes, will contain part of the data

And you send more packets like this.

Or, to make it simpler, you could use RakNet which is free for non-commercial use (And quite easy after getting used to it).

http://www.raknet.com/raknet/downloads/
Last version:
http://www.raknet.com/raknet/downloads/RakNet_PC-4.07.zip
(30/01/2013)
No, no I didn't mean that it would be a good idea to not limit the packets, what I meant was that I am going to add it as one of the choices in the setup file that comes with the program. I will have a recommended value of 512 - 1024 based on internet connection and what not but I don't what to use an HTML server, truthfully, I don't know what that is. but for now the to things above are my only issues. This isn't supposed to be a huge thing like skype, but more of a small chat room for starter games or if some group of people think the idea is neat and want to use this instead of skype. I am trying to give it the most "Window Console"y look as possible. I might make a newer version at a later time made with directx and some dll injection (for games only) so its a part of the program using it. but thats look in the future. for now I need getline to stop skipping user input and there needs to be no waiting for the other person to message. Fix those for me and I will forever be in your debt ;)
Oh well let me see.

You're simply trying to be able to both type and receive data at the same time I guess?
Or, even just send two messages one after another?
Unless you use threads, you're not able to do it (Unless you use non-blocking sockets, but I know almost nothing about them, and I think I'll be looking into them like next week with my schedule).

Again, the simplest way to do it is with RakNet, it will handle threaded shyte on his own, while you can do whatever you want.

If you don't know what a thread is, it's like a "program flow".
You have a main thread (who starts at "int main()") and you can create more different threads (say you create a thread function "int mythread()" it will run in the same time as "main", but it won't block "main". They will both run at the same time. Any big issues with this? Yes, if you access the same variables at the same time, you don't know what could happen).

FYI (For your information) an HTTP server is simply a "website".
It's what you can "surf" with a web browser.

I don't think you should switch between 1024 and 512 (Remember they're in bytes, they can also be a higher value like 4096 but you should use new and delete to avoid using too much stack space [resulting in a crash])

But you still need some simple threading in your program.
Seeing we're on Windows section:

http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx

(Threading is quite hard if you're a beginner, you should try it on another project first to make sure you understand how it works)
I think I get what you said I should do, I should have one function (int main()) receiving and display the messages, while another function (int myThread()) checks if the user is trying to send a message? that seems logical.

oooohh, I feel dumb I know what a website is, I guess I just couldnt grasp what you were saying.

if all else fails, I will look into RakNet

Thanks again!
Huh, yep, that's what I meant.
And I expected you understood what a website is, and you was just distracted and didn't understand me straight away.
Topic archived. No new replies allowed.