Hi all...
I am looking for examples of how to create a WinSock server. I would like for a telnet client to connect to the server. The server first displays text and provides a prompt for user input from the client. Can someone guide me in the right direction or provide a piece of code I can work with? I've googled until my guts fall out. I am building on a Win32 platform using Visual C++ Studio Community 2017.
Here's my code, trying to output using a socket:
char c;
std::fstream myStream("welcome.txt", std::fstream::in);
while (myStream.get(c))
{
send(clientSocket, (const char*)c, size_t(0), 0);
std::cout << c;
Here's my code, trying to output using a socket:
char c;
std::fstream myStream("welcome.txt", std::fstream::in);
while (myStream.get(c))
{
send(clientSocket, (const char*)c, size_t(0), 0);
std::cout << c;
Where/how is clientSocket declared? And has it been set up with a valid socket handle? That isn't shown in your code snippet. It looks like this code is reading from a text file and trying to send each character from the file over a socket?
Also, did you intend for the "std::cout << c" to be indented? It doesn't need to be, as it's not in a separate block of code.
Indentations has no meaning in source code except cosmetically.
Re: WinSock
By: Digital Man to Jon Justvig on Sat Jun 09 2018 08:08 pm
DM,
Okay, I figured out send() just fine. I'm having major issues with recv(). Right now, I can only get it to input one character, even with char arrays or strings. Here's a sample:
int result;
// our recv loop
while (true)
{
result = recv(current_client, buffer, sizeof(buffer), 0); // recv cmds
Sleep(10);
if (result > 0)
{
cout << "\n\tMessage from client: " << buffer;
send(current_client, (const char*)buffer, sizeof(buffer), 0);
}
Okay, I figured out send() just fine. I'm having major issues with
recv(). Right now, I can only get it to input one character, even with
char arrays or strings. Here's a sample:
int result;
// our recv loop
while (true)
{
result = recv(current_client, buffer, sizeof(buffer), 0); // recv cmds
Sleep(10);
if (result > 0)
{
cout << "\n\tMessage from client: " << buffer;
send(current_client, (const char*)buffer, sizeof(buffer), 0);
}
Re: WinSock
By: Digital Man to Jon Justvig on Tue Jun 12 2018 11:32 am
Okay, I figured out send() just fine. I'm having major issues with
recv(). Right now, I can only get it to input one character, even with
char arrays or strings. Here's a sample:
char buffer[20];
int result;
// our recv loop
while (true)
{
result = recv(current_client, buffer, sizeof(buffer), 0); // recv cmds
Sleep(10);
if (result > 0)
{
cout << "\n\tMessage from client: " << buffer;
send(current_client, (const char*)buffer, sizeof(buffer), 0);
}
just above int result; ...
Indentations has no meaning in source code except cosmetically.
Yes, but indentations in C/C++ are typically done for separate code blocks. If you indent code randomly, then it is more difficult to read.
Re: WinSock
By: Jon Justvig to Nightfox on Mon Jun 11 2018 05:56 pm
Indentations has no meaning in source code except cosmetically.
Yes, but indentations in C/C++ are typically done for separate code blocks. If you indent code randomly, then it is more difficult to read.
Nightfox
char buffer[20];
int result;
// our recv loop
while (true)
{
result = recv(current_client, buffer, sizeof(buffer), 0); // recv
cmds
Sleep(10);
if (result > 0)
{
cout << "\n\tMessage from client: " << buffer;
send(current_client, (const char*)buffer, sizeof(buffer), 0);
}
just above int result; ...
So.. if you only receive 2 bytes (result == 2), you're still going to send 20 (sizeof buffer)? That's probably not what you want.
Re: WinSock
By: Digital Man to Jon Justvig on Tue Jun 12 2018 05:29 pm
char buffer[20];
int result;
// our recv loop
while (true)
{
result = recv(current_client, buffer, sizeof(buffer), 0); // recv
cmds
Sleep(10);
if (result > 0)
{
cout << "\n\tMessage from client: " << buffer;
send(current_client, (const char*)buffer, sizeof(buffer), 0);
}
just above int result; ...
So.. if you only receive 2 bytes (result == 2), you're still going to send 20 (sizeof buffer)? That's probably not what you want.
How might I receive between 2 and 12 bytes?
Re: WinSock
By: Digital Man to Jon Justvig on Wed Jun 13 2018 12:24 pm
SOCKET current_client = (SOCKET)lpParam;
// buffer to hold our recived data
char buffer;
// buffer to hold our sent data
char sendData[20];
// for error checking
int result;
// our recv loop
while (true)
{
result = recv(current_client, (char*)&buffer, 1, 0); // recv cmds
Sleep(10);
if (result > 0)
{
cout << "\n\tMessage from client: " << buffer;
send(current_client, (char*)&buffer, 1, 0);
}
}
}
Right now, it works, however, on the client side it only shows one character at at time with Message from client: (character the remote end sent) ...I tried using a char array, however, it was just showing back garbage. How might I get input up to about 12 characters until the "\r" (carriage return) is received to the server from the remote client?
Sysop: | Chris Crash |
---|---|
Location: | Huntington Beach, CA. |
Users: | 577 |
Nodes: | 8 (0 / 8) |
Uptime: | 72:29:49 |
Calls: | 10,734 |
Files: | 5 |
Messages: | 442,755 |