C++ Server reply properly - c++

I want to make a Server that reply to my Sockets.
I have a code like this:
#define DEFAULT_BUFLEN 512
/*...*/
int iResult;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
/*...*/
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
if (recvbuf == "hello"){
iSendResult = send(ClientSocket, "Hi Client", sizeof("Hi Client"), 0);
}else {printf("[ERROR]Unexpected Socket.\n"); }
Now, it doesn't work. and I don't now why. I try to searck something online (whit poor results).
How can I make it works? I'm willing to change all the code.

You can't compare C-style strings with ==. You're comparing the address of the buffer with the address of a static string literal, which will always be unequal.
You also need to deal with the fact that each read from a stream socket (assuming that's what this is) might give more or less data than you're expecting.
A more correct comparison might be
if (iResult < 0) {
// Some kind of read error, check errno for details
} else if (iResult == 0) {
// Socket was closed
} else if (iResult < 5) {
// Not enough input: read some more or give up
} else if (std::equal(recvbuf, recvbuf+5, "hello")) {
// Expected input: there might be more data to process
} else {
// Unexpected input
}

Related

sql server has gone away error because of WSACleanup

I would like to ask for help since I don't know what to do anymore. I have a simulator created in c++, where it accepts an id input from a user and checks if it is in the database (created in mysql workbench) which is only in the localhost.
sqlQuery = "SELECT staffaccess.card_number FROM proxycardsim.staffaccess WHERE staffaccess.card_number = "
+ inputID;
if(mysql_ping(theInstance.connects))
{
}
int queryState = mysql_query(theInstance.connects, sqlQuery);
resultSet = mysql_store_result(theInstance.connects);
rowNum = mysql_num_rows(theInstance.resultSet);
if (rowNum == NULL)
{
mysql_free_result(theInstance.resultSet);
return false;
}
else
{
mysql_free_result(theInstance.resultSet);
return true;
}
The thing is that the simulator is connected to another computer that serves as a server (connected through winsock). If the server is up, it works ok or if all the inputs are wrong, but if the server is down(my code will try to connect again to the server pc so I have to call WSACleanup) after inputting one correct value and I input another mysql_query returns an error that mysql server has gone away. Then the program will break when it goes to mysql_num_rows.
I have this code in another function and when I commented them out one by one, I found out that the error is because of WSACleanup(). If the WSACleanup line is not there my query runs ok.
if ( false == theInstance.compareID(m_IDEntry))
{
addData(ConsoleLog,rec,0,0);
}
else
{
// Send an initial buffer
iResult = send( connectSocket, sendBuf, (int)strlen(sendBuf), 0 );
if(false == theInstance.addToLog(m_IDEntry))
{
addData(ConsoleLog,rec,0,3);
}
if (iResult == SOCKET_ERROR) {
closesocket(connectSocket);
WSACleanup();
serverConnect();
iResult = send( connectSocket, sendBuf, (int)strlen(sendBuf), 0 );
}
if (iResult != SOCKET_ERROR) {
addData(ConsoleLog,rec,0,1);
}
iResultRcv = recv(connectSocket, recvbuf, recvbuflen, 0);
if ( iResultRcv <= 0 )
{
addData(ConsoleLog,rec,0,7);
}
}
I hope someone can help me out.
Don't call WSACleanup. WSACleanup is intended to be used when you no longer want to do any socket communication. That's not the case for you.

Recv() Function Hangs After Sending HTTP GET Request in Winsock in C++

I am trying to make a program that uses HTTP in winsock, but I have run into a problem where the recv function just hangs there.
int connect()
{
WSADATA t_wsa; //WSADATA structure
WORD wVers = 0x0202; //version number
int iError; //error number
wVers = MAKEWORD(2, 2); // Set the version number to 2.2
iError = WSAStartup(wVers, &t_wsa); // Start the WSADATA
if(iError != NO_ERROR || iError == 1)
{
printf("Error at WSAStartup()\n");
WSACleanup();
system("PAUSE");
return 1;
}
/* Correct version? */
if(LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2)
{
printf("Incorrect version\n");
WSACleanup();
system("PAUSE");
return 1;
}
SOCKET sClient;
sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sClient == INVALID_SOCKET || iError == 1)
{
printf("Invalid Socket!\n");
WSACleanup();
system("PAUSE");
return 1;
}
SOCKADDR_IN sinClient;
memset(&sinClient, 0, sizeof(sinClient));
char cIP[50];
strcpy_s(cIP, "98.139.183.24");
sinClient.sin_family = AF_INET;
sinClient.sin_addr.s_addr = inet_addr(cIP); // Where to start server
sinClient.sin_port = htons(80); //Port
if(connect(sClient, (LPSOCKADDR)&sinClient, sizeof(sinClient)) == SOCKET_ERROR)
{
/* failed at starting server */
printf("Could not connect ot the server!\n");
WSACleanup();
system("PAUSE");
return 1;
}
// Now we can send/recv data!
printf("YOU ARE CONNECTED!\r\n");
string buffer;
buffer += "GET / HTTP/1.1\r\n";
buffer += "Host: http://www.yahoo.com/\r\n";
buffer += "Connection: close\r\n\r\n";
const char *cha = buffer.c_str();
int sent;
int response;
sent = send(sClient, cha, sizeof(cha) - 1, 0);
char recvbuf[50000];
response = recv(sClient, recvbuf, 50000, 0);
recvbuf[response] = '\0';
printf("\nReceived data = %s", recvbuf);
WSACleanup();
return(0);
}
"sent" will get printed after the send function, but nothing after recv gets printed.
What am I missing here?
A possible cause is that the send() is not sending the data intended:
sent = send(sClient, cha, sizeof(cha) - 1, 0);
the sizeof(cha) - 1 is actually sizeof(char*) - 1, not the actual length of the data: use buffer.length() instead.
Note that you can construct the std::string with the string literal in a single statement instead of constructing it via several concatentations. However, as the std::string is being used to obtain a const char* only there is no reason for using std::string at all:
const char* buffer = "GET / HTTP/1.1\r\n"
"Host: http://www.yahoo.com/\r\n"
"Connection: close\r\n\r\n";
sent = send(sClient, buffer, strlen(buffer), 0);
Check the return value of send() and recv() to determine success or failure, particularly recv() as the result is being used to index an array. On failure, recv() returns SOCKET_ERROR which (I think) is -1.
Handling HTTP responses correctly requires significant effort. The receiving code needs to examine the returned HTTP headers to determine how to handle the response content. For example, a HTTP response may be chunked or not. Libraries exist for managing HTTP requests, one is cpp-netlib (which was announced on isocpp.org circa February 2013).

sending whole data at once using socket

I'm trying to send a file from client to server using winsock2 lib.
After converting the file into char array, i'm sending this array using the send() command.
The problem: the data sent separately.
For e.g: I have file of size: 144429.
It does not send it at once, the data is split into many portions, like:
first send: 1460
second send: 21544
third send: 57136
etc..
until to whole data is sent.
so my question is: what causes it to send it by parts and not by once????
Solution I found working but not making any sense:
If i'm adding
cout << "bla bla bla";
before the send() function, it does work and send the whole 144429 by once. (but if the string given to cout is shorter, no change, send by parts)
CODE:
CLIENT SIDE
int Client::sendData(char *sendbuf, int length)
{
int iResult;
// if I remove those next few lines (until and including the cout line)
// the send will split.
char tmp[1];
// sent 1 dummy byte
iResult = send( _connectSocket, tmp, 1, 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
return closeSocket();
}
cout << "SENDING DATA..." << endl;
// THIS IS THE RELEVANT AND ACTUAL DATA I WANT TO SEND
// send the data
iResult = send( _connectSocket, sendbuf, length, 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
return closeSocket();
}
cout << "Data sent (" << iResult << " Bytes)" << endl;
return 0;
}
SERVER SIDE:
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
int iResult = 0;
int totalBytesRead = 0;
// Receive until the peer shuts down the connection
do {
totalBytesRead += iResult;
iResult = recv(_clientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0) {
printf("RECEIVED DATA\n");
printf("Bytes received: %d\n", iResult);
} else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed: %d\n", WSAGetLastError());
closesocket(_clientSocket);
WSACleanup();
return 1;
}
} while (iResult > 0);
// store data into file
FileTransfer::binaryToFile(recvbuf, "doch.docx", totalBytesRead-1);
return 0;
}
There is no way to guarantee that send transmits some data as one unit - it just doesn't work that way. You have to add some extra information to tell the system that "Here's this much data to come" and/or "I'm done now". Even if you could convince your sending side to send of everything in one packet, assuming the receiving side isn't connected DIRECTLY with just a simple cable to the sender, you can't guarantee that the packet isn't broken up during it's passing through the network.
You just have to accept that if you are sending more than a single byte in a packet, you may have to call send multiple times. To simplify it, write a function that takes a an arbitrary size "whole packet" and calls send as many times as necessary... If you have a protocol that indicates the size of the data sent [such as in the first few bytes], you could have a receive function that does the same thing.
Depending on the socket type you are using there might be a limitation of data size in the underlying transport protocol.
In case you are using a network socket the size is limited by the maximum transfer unit (see: http://en.wikipedia.org/wiki/Maximum_transmission_unit).
If your data doesn't fit this size you will have to iterate in a loop sending portions of your data until either an error occurs or all data has been sent.

BSD Sockets - Using send and recv

I am trying to implement a simple chat program in linux using bsd sockets. Right now I am just trying to send and receive one message to the server from a client. Whenever I run the code, recv returns -1 and the errno code is 22.
Server code -
struct sockaddr name;
char buf[80];
int main(int agrc, char** argv) {
int sock, new_sd; //sock is this socket, new_sd is connection socket
int adrlen, cnt;
name.sa_family = AF_UNIX;
strcpy(name.sa_data, "/tmp/servsock");
adrlen = strlen(name.sa_data) + sizeof(name.sa_family);
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
cout<<"\nserver socket failure "<<errno;
cout<<"\nServer: ";
exit(1);
}
unlink("/tmp/servsock");
if(bind (sock, &name, adrlen) < 0)
cout<<"\nBind failure "<<errno;
if(listen(sock, 5) < 0)
cout<<"\nlisten error "<<errno;
while(1) {
if( new_sd = accept(sock, &name, (socklen_t*)&adrlen) < 0) {
cout<<"\nserver accept failure "<<errno;
exit(1);
}
char* buf = new char[14];
if(recv(sock, buf, 14, 0) < 0) {
cout<<"\nError receiving data "<<errno;
exit(1);
}
} //end while
return 0;
}
Client code -
struct sockaddr name;
int main(int agrc, char** argv) {
int sock, new_sd, adrlen, cnt;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
cout<<"\nserver socket failure "<<errno;
cout<<"\nServer: ";
exit(1);
}
//stuff for server socket
name.sa_family = AF_UNIX;
strcpy(name.sa_data, "/tmp/servsock");
adrlen = strlen(name.sa_data) + sizeof(name.sa_family);
if(connect(sock, &name, adrlen) < 0) {
cout<<"\nclient connection failure "<<errno;
exit(1);
}
cout<<"\nSuccessful connection from client 1";
std::string buf = "\nClient 1 Here";
if(send(sock, buf.c_str(), strlen(buf.c_str()), 0) < 0) {
cout<<"\nError sending data from client 1 "<<errno;
exit(1);
}
cout<<"\nExiting normally";
return 0;
}
Even though I get the error on the server side, I do not get the error message on the client side - it just exits normally.
According to - http://www.workers.com.br/manuais/53/html/tcp53/mu/mu-7.htm the errno 22 error message just means "Invalid argument". But I don't know how exactly to interpret that...if an argument was invalid why would it even compile?
If anyone can point out what I'm doing wrong here I would be very grateful. And any other small notes you feel like pointing out would be welcomed. Thanks for any help.
Aside from all other problems in your code, you are trying to read on the wrong file descriptor - it should be new_sd, not sock, which is a server socket and can only accept() new connections.
Edit 0:
Big boo-boo:
if( new_sd = accept(sock, &name, (socklen_t*)&adrlen) < 0) { ...
This is equivalent to:
if( new_sd = (accept(sock, &name, (socklen_t*)&adrlen) < 0)) {
So new_sd gets totally wrong value. General wisdom is not to put assignments into conditionals. Consider compiling with high warning levels, at least -Wall -pedantic.
One thing that looks wrong in your code is that you're recving on sock when you should be recving from new_fd. Not sure why that would give EINVAL though.
(EINVAL errors are (usually) not detectable at compile time. File descriptors are plain ints. The compiler cannot know which ints are valid file descriptors at runtime, or if a particular combination of flags is valid for the sockets you're using for instance.)
In the'recv()' call (in the server), the 'flags' parameter can't be 0:
recv(sock, buf, 14, 0)
Try something like:
recv(sock, buf, 14, MSG_WAITALL)
See the 'man' page for the whole list of options for 'flags' parameter. One must be judicious here on how the message is to be received.
The reason why the client doesn't get the error message (INVALID ARG) is because it doesn't do any recv's ... only the server is doing receive's.

bind: Socket operation on non-socket

I writing a server and a client and keep getting 'bind: Socket operation on non-socket'.
I've researched the heck out of this, have other code that runs in another application and have exhausted 8 hours trying to find this bug.
The code is:
void TCPSocket::buildTCPSocket(int port)
{
initializeSocket1();
getSocket();
bindSocket();
listenToSocket();
acceptSocket();
// now you can send() and recv() with the
// connected client via socket connectedTCPSocket
}
void TCPSocket::getSocket()
{
// Get an internet domain socket AF_INET
if(socket1 = socket(AF_INET, SOCK_STREAM,0) == -1)
{
perror("socket");
exit(1);
}
}
void TCPSocket::bindSocket()
{
// Bind to a port on the host
int myAddressSize = sizeof(myAddress);
int bindReturnValue = bind(socket1, (struct sockaddr *) &myAddress, AddressSize);
if (bindReturnValue == -1)
{
perror("bind"); // <== Error message generated here
exit(1);
}
printf("Socket for TCP bound to port %d\n", port);
}
Also, prior to this, I memset the memory block with this function.
void TCPSocket::initializeSocket1()
{
// Fill tcpSocket struct with 0's
memset(&myAddress, '\0', sizeof(myAddress));
myAddress.sin_family = AF_INET;
myAddress.sin_addr.s_addr = INADDR_ANY;
// Conver PORT to big-endian if necessary
myAddress.sin_port = htons(this->port);
}
Variables are declared in the header file of the class.
public:
struct sockaddr_in myAddress, clientAddress;
void buildTCPSocket(int newPort);
private:
int port;
int socket1, socket2;
socklen_t clientAddressLength;
-- Edit the code should be a little more clear now. socket1 is initialized in getSocket().
I've seen where a bunch of guys have missed the parens in the if but I think I eliminated that error by declaring myAddressSize and bindReturnValue.
Any input is appreciated.
Thank you,
Ted S
Ok, problem solved. Of course the problem is never where you are looking are you would have found it. Here is the corrected code. The problem was in a missing set of parens in the call to socket().
void TCPSocket::getSocket()
{
// Get an internet domain socket AF_INET
if((socket1 = socket(AF_INET, SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(1);
}
}
Thanks again!
I can almost guarantee you that you're getting that error because you never initialized socket1.
Typically you have to do something like this:
int socket1 = socket(AF_INET, SOCK_STREAM, 0);
bind(socket1, ...);
I don't see any code anywhere in there for setting up socket1. This is what the error message is telling you, after all. socket1 isn't a socket, so it's failing.
Edit: As a follow up, this is one of the reasons why I try to avoid using the syntax
if ((foo = bar()) == ERROR)
{
// handle me
}
And instead stick with:
void TCPSocket::getSocket()
{
// Get an internet domain socket AF_INET
socket1 = socket(AF_INET, SOCK_STREAM, 0);
if (socket == -1)
{
perror("socket");
exit(1);
}
}