Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » windows sockets programming
windows sockets programming [message #1797529] Thu, 01 November 2018 08:32
fdsfd dfdsc is currently offline fdsfd dfdscFriend
Messages: 1
Registered: November 2018
Junior Member
Hi guys, first post here please let me know if ive posted in the wrong place or something and ill move it? Im just looking for some help with my program. I have a server and a client set up connecting through sockets and i have a basic message system setup where i send a message from server to client and the then the client echos that message back to server. I am trying to add to it so that i can open a command prompt session on client and then send commands from server to client and then output from client is send back to server. My current message system from server and client is below. Any help is hugely appreciated. Thanks

server message code:

// Do-while loop to send and receive data
char buf[4096];
string userInput;

do
{
// Prompt the user for some text
cout << "> ";
getline(cin, userInput);

if (userInput.size() > 0) // Make sure the user has typed in something
{
// Send the text
int sendResult = send(clientSocket, userInput.c_str(), userInput.size() + 1, 0);

if (sendResult != SOCKET_ERROR)
{
// Wait for response
ZeroMemory(buf, 4096);
int bytesReceived = recv(clientSocket, buf, 4096, 0);
if (bytesReceived > 0)
{
// Echo response to console
cout << "CLIENT> " << string(buf, 0, bytesReceived) << endl;
}
}
}

} while (userInput.size() > 0);

client message code:

// While loop: accept and echo message back to server
char buf[4096];
while (true)
{
ZeroMemory(buf, 4096);

// Wait for server to send data
int bytesReceived = recv(sock, buf, 4096, 0);
if (bytesReceived == SOCKET_ERROR)
{
cerr << "Error in recv(). Quitting" << endl;
break;
}

if (bytesReceived == 0)
{
cout << "Client disconnected " << endl;
break;
}

cout << string(buf, 0, bytesReceived) << endl;

// Echo message back to server
send(sock, buf, bytesReceived + 1, 0);

}
Previous Topic:Code analysis issues (unresolved symbols
Next Topic:How to configure preprocessors in eclipse CDT makefile project
Goto Forum:
  


Current Time: Thu Apr 25 22:46:20 GMT 2024

Powered by FUDForum. Page generated in 0.02510 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top