SOCKET communication through C++ on Win7 with other than English OS languages - c++

I have a project where I communicate with the server through socket and is running for long period.(Source code below...) However on win 7 with languages as Danish/Chinese on some machines it gives me erorrs like: a particular servelet doesn't exist. Although the connection is created and message is sent but I receive back the message as an error.
Now I want to include winsock2.h with Ws2_32.lib and want to change MAKEWORD(1,1) to MAKEWORD(2,2). How far it is safe for me to go with this change with a project which is running since a long period?
// I have included the basic sources here.
#include <winsock.h>
// with winsock32.lb
SOCKET sockRef = NULL;
struct sockaddr_in srvInfo;
struct hostent *netHostForName = NULL;
WORD wVersionRequested = MAKEWORD(1,1);
int nRet = WSAStartup(wVersionRequested, &wsaData);
netHostForName = gethostbyname(serverName);
memcpy((char *)&srvInfo.sin_addr, netHostForName->h_addr,
netHostForName->h_length);
srvInfo.sin_port = htons(portNumber);
srvInfo.sin_family = AF_INET;
sockRef = socket(AF_INET, SOCK_STREAM, 0);
connect(sockRef, (struct sockaddr *)&srvInfo,sizeof(srvInfo));
// Sending messages with
SockWrite(Socket, message, strlen(message), 0);
//Receiving messages with
recv(Socket, szBuffer, sizeof(szBuffer), 0);
CLOSESOCKET(Socket);
WSACleanup();

its not the socket , its the data , use utf-8 encoded data as char stream.

Related

Asynchronous data reading from server using WinSock

I am using the WinSock API in C++ to connect and send data to a specific server (to increase readability, all the code is without error handling):
#include <winsock2.h>
#include <Windows.h>
#define PORT 5051
WSADATA WsaData;
WSAStartup(MAKEWORD(2, 2), &WsaData);
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN sin;
ZeroMemory(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr("Some valid IP");
connect(s, (sockaddr *) &sin, sizeof(sin));
string sdata_out = "Something like GET request\n";
send(s, sdata_out.c_str(), sdata_out.size(), 0);
The server will start to sending responses, separated by different periods of time. It will send responses without additional requests from me.
I need to read this data asynchronously (not cycle of recv() functions).
I tried to use WSAEventSelect() to listen on the local socket, like this:
SOCKET sloc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN sinloc;
ZeroMemory(&sinloc, sizeof(sinloc));
sinloc.sin_family = AF_INET;
sinloc.sin_port = htons(PORT);
sinloc.sin_addr.s_addr = INADDR_ANY;
WSAEventSelect(sloc, EventArray[EventTotal - 1], FD_ACCEPT |
FD_CLOSE | FD_READ | FD_CONNECT) == SOCKET_ERROR)
bind(sloc, (sockaddr *)&sinloc, sizeof(sinloc));
listen(sloc, 5));
I have no idea what to do further. I tried following this example, but my program got stuck at runtime on this code:
DWORD Event = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE, WSA_INFINITE, FALSE)
What is the problem? How should my event handler function look? Or, am I am doing something fundamentally wrong? Is there a correct way to asynchronously read data from the server, if I don't know when the data comes?

Windows UDP sockets: recvfrom() fails with error 10054

Hello everyone.
I'm trying to use Windows sockets to send and receive UDP packets (in C++).
It worked well until three days ago, when the program stopped behaving properly.
To summarize the situation:
When calling WSAPoll() on my socket, it always returns my socket updated with EVERY revents possible (corresponding to every events I gave the pollfd), even if there is no server launched.
When calling recvfrom() and no server is launched, it returns SOCKET_ERROR with error code 10054(*).
When calling recvfrom() and a server is launched, it works properly - blocks until it receives something.
The behavior is the same whether I try to connect to localhost or to a distant host.
(*) I investigated this error. In UDP, it means that there is an ICMP problem. ("On a UDP-datagram socket this error indicates a previous send operation resulted in an ICMP Port Unreachable message.").
I indeed call sendto() before recvfrom(), so the problem's not here.
I tried to put down my firewall to see if it changed anything, but it didn't. I also tried to put down every network flowing through my PC. In this state I managed to get the program to work for a few minutes, but when I enabled the networks it stopped working again. I tried to repeat the process but it would not work anymore.
I tried compiling with both visual studio (2015) and MinGW.
I tried on another computer too (under Windows 7, mine has Windows 8.1), to no avail.
Here is a simple test file which does not work on my computer.
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x501
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <vector>
#include <iostream>
int main() {
int clientSock;
char buf[100];
int serverPort;
/* Initializing WSA */
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
/* I create my socket */
struct addrinfo specs;
struct addrinfo *addr = new addrinfo;
ZeroMemory(&specs, sizeof(specs));
specs.ai_family = AF_INET;
specs.ai_socktype = SOCK_DGRAM;
specs.ai_flags = 0;
getaddrinfo("127.0.0.1", "2324", &specs, &addr);
clientSock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
/* I get the server's address */
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(2324);
int len = sizeof(struct sockaddr);
/* I'll poll & recvfrom on my socket */
std::vector<pollfd> fds;
pollfd fd;
fd.fd = clientSock;
fd.events = POLLRDNORM;
fd.revents = -1;
fds.push_back(fd);
while(1) {
memset(buf,0,sizeof(buf));
printf("\nClient--->: ");
gets(buf);
/* It's UDP, so it doesn't matter if there is someone to receive the packet */
sendto(clientSock, buf, strlen(buf), 0, (sockaddr*)&serverAddr ,len);
memset(buf,0,sizeof(buf));
int ret;
/* Always returns "1" */
if ((ret = WSAPoll(fds.data(), 1, 0)) > 0) {
std::cout << ret;
/* Always returns "-1" */
std::cout << recvfrom(clientSock,buf,sizeof(buf),0, (sockaddr*)&serverAddr,&len) << std::endl;
printf("\n--->From the server: ");
printf("%s",buf);
}
}
closesocket(clientSock);
WSACleanup();
return 0;
}
Two questions:
Why does WSAPoll() always returns an updated socket, even if there wasn't any interaction with it ?
Why does recvfrom() return this error and how can I fix it ? I suppose it comes from my computer. I tried allowing ICMP through my firewall but it didn't change anything, maybe I did something wrong ?
Edit: I fixed my main program (not shown here because it is way too large) by just ignoring any "error 10054" I received. Now it works the same way it does on Unix.
Still, it is not really a solution (ignoring an error code... meh) and if anyone knows why I get the "ICMP Port Unreachable" error when calling sendto(), I'd be glad to hear about it.
In Windows, if host A use UDP socket and call sendto() to send something to host B, but B doesn't bind any port so that B doesn't receive the message, and then host A call recvfrom() to receive some message, recvfrom() will failed, and WSAGetLastError() will return 10054.
It's a bug of Windows. If UDP socket recv a ICMP(port unreachable) message after send a message, this error will be stored, and next time call recvfrom() will return this error.
There are 2 ways to solve this problem:
Make sure host B has already bound the port you want to send to.
Disable this error by using following code:
#include <Winsock2.h>
#include <Mstcpip.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR, 12)
BOOL bNewBehavior = FALSE;
DWORD dwBytesReturned = 0;
WSAIoctl(iSock, SIO_UDP_CONNRESET, &bNewBehavior, sizeof bNewBehavior, NULL, 0, &dwBytesReturned, NULL, NULL);
Reference:
http://www.cnblogs.com/cnpirate/p/4059137.html
I have stripped down the Authors code and included the fix of simmerlee. This provides an simpler way to reproduce the error:
#include "stdafx.h"
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR, 12)
void testCase(bool fixed)
{
int clientSock;
char rcvBuf[100];
// create socket
clientSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(fixed)
{
BOOL bNewBehavior = FALSE;
DWORD dwBytesReturned = 0;
WSAIoctl(clientSock, SIO_UDP_CONNRESET, &bNewBehavior, sizeof bNewBehavior, NULL, 0, &dwBytesReturned, NULL, NULL);
}
// bind socket
struct sockaddr_in clientAddr;
clientAddr.sin_family = AF_INET;
clientAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
clientAddr.sin_port = htons(61234);
int sizeClientAddr = sizeof(clientAddr);
bind(clientSock, (sockaddr*) &clientAddr, sizeClientAddr);
struct sockaddr_in serverAddr = clientAddr;
serverAddr.sin_port = htons(2324); // change port where nobody listens
int sizeServerAddr = sizeof(struct sockaddr);
int lasterror = 0;
int status = 0;
// send where nobody is listening
printf("Send to nowhere--->:\n");
/* It's UDP, so it doesn't matter if there is someone to receive the packet */
status =sendto(clientSock, "Message", 7, 0, (sockaddr*)&serverAddr, sizeServerAddr);
lasterror = WSAGetLastError();
printf("sendto return %d (lasterror %d)\n", status, lasterror);
// recvfrom with "failing" sendto before.
// fixed: This should block.
// unfixed: WSAGetLastError is 10054
memset(rcvBuf, 0, sizeof(rcvBuf));
status = recvfrom(clientSock, rcvBuf, sizeof(rcvBuf), 0, (sockaddr*)&serverAddr, &sizeServerAddr);
lasterror = WSAGetLastError();
printf("recvfrom return %d (lasterror %d)\n", status, lasterror);
printf("--->From the server: -%s-\n", rcvBuf);
closesocket(clientSock);
}
int _tmain(int argc, _TCHAR* argv[])
{
/* Initializing WSA */
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
printf("##### UNFIXED\n");
testCase(false);
printf("##### FIXED\n");
testCase(true);
WSACleanup();
// pause
char buf[100];
gets(buf);
return 0;
}
This should return:
##### UNFIXED
Send to nowhere--->:
sendto return 7 (lasterror 0)
recvfrom return -1 (lasterror 10054)
--->From the server: --
##### FIXED
Send to nowhere--->:
sendto return 7 (lasterror 0)
and then block.

sendto and recvfrom in the same program?

this is my first time asking a question, and the first time I couldn't find an answer by just searching this site. so please go easy on me if I am doing it wrong.
Using Winsock. I need to send AND receive a packet of information to another computer running the same program. the type of connection should be UDP(non blocking I guess?)and using a data-coupled model.
do I need to be sending and receiving the information using different threads?
I know the data sends just fine but it is not being received at all.
I can easily have on program send and a completely different program recv but it seems the principles don't carry over to what I am trying to do.
should I use the same sockaddr_in struct for the recvfrom and sendto? or can they use the same one? how about slen? doesn't matter I have tried both and neither work.I have tried using one port to send and one to recieve, I have tried having one port to do both. nothing. I am relatively new to Winsock so sorry if I sound hopeless or missing something horribly obvious. At this point I just really need some help or at least a point in the right direction I don't care how many ports I just want to see the recv printf incoming with the right data.
winsock errors are
bind failed 10048 and recv failed 10022
I will also mention I looked into a concept called setsockopt suggested by MSDN but it just ended up furthering my confusion. am I missing something?
here are some relevant functions.
void UDPNetwork::initserver()
{
initRemote();
slen2= sizeof(si_other);
if ((_recvSocket = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
printf("failed to create socket: %d", WSAGetLastError());
}
_recvSocket = (AF_INET, SOCK_DGRAM, 0);
memset((char *)&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
if (bind(_recvSocket, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d",
WSAGetLastError());
/* system("PAUSE");
exit(EXIT_FAILURE);*/
}
}
void UDPNetwork::initclient(){
slen = sizeof(si_other);
_sendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (_sendSocket == SOCKET_ERROR)
{
printf("failed to create socket... ");
exit(EXIT_FAILURE);
}
memset((char *)&si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);
}
void UDPNetwork::send(float x, float y, float z)
{
sprintf(bufOut,"%f,%f,%f", x, y, z);
if (sendto(_sendSocket, bufOut, strlen(bufOut), 0, (struct sockaddr*) &si_other, slen) == SOCKET_ERROR)
{
printf("sento() failed with error code : d%", WSAGetLastError());
exit(EXIT_FAILURE);
}
//printf("Sent: %s\n", bufOut);
}
void UDPNetwork::recv(char *msg)
{
float ax = 0; float ay = 0; float az = 0;
memset(msg, '\0', BUFLEN);
recv_len = recvfrom(_recvSocket, msg, BUFLEN, 0, (struct sockaddr*) &si_other, &slen);
int nError = WSAGetLastError();
if (nError != WSAEWOULDBLOCK&&nError != 0);
{
printf("Recv failed with error code : %d",
WSAGetLastError());
//system("PAUSE");
//exit(EXIT_FAILURE);
}
recv = buf;
sscanf_s(msg, "'%f,%f,%f", &ax,&ay,&az);
printf("RECV: %f %f %f\n", ax, ay, az);
}
Most UDP receivers will send data back to the same IP:Port that the reeived data was sent from. So by creating two different sockets, you are using two different IP:Port pairs, which is likely why you are not receiving data. The data is likely being sent to a different IP:Port than the one you are reading on.
For UDP, you usually do not need to create separate send and receive sockets. You can create one UDP socket and use it for both sending and receiving, so the sending IP:Port matches the receiving IP:Port. The only time you should need to create separate sockets is if the receiver intentionally sends its data to a different IP:Port than what the sending socket is using.
after searching deep into the MSDN archives I found this helpful little page
https://msdn.microsoft.com/en-us/library/bb530747%28v=vs.85%29.aspx
hopefully this will add some insight to my problem. I will update on weather this resource ended up solving my problems

Receiving multiple messages Winsock2 C++

I am trying to make a program using Winsock2 where I can send a message from client to server. I can do this, but the problem is I can only send one message and then I have to restart the server to receive the next message. Here is the code for the server. The part I am really confused about is I have the recv() function in a while loop so why isn't it continuing to "receive" data?
WSADATA wsaData;
int bytes_recieved;
char data_recieve[2048];
string output;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
SOCKADDR_IN ServerInfo;
ServerInfo.sin_family = AF_INET;
ServerInfo.sin_addr.s_addr = INADDR_ANY;
ServerInfo.sin_port = htons(8888);
bind(ListenSocket, (LPSOCKADDR)&ServerInfo, sizeof(struct sockaddr));
listen(ListenSocket, 1);
SOCKET ClientSocket = accept (ListenSocket, NULL, NULL);
while(true)
{
bytes_recieved = recv(ClientSocket, data_recieve, 2048, 0);
if (bytes_recieved > 1)
{
cout << data_recieve;
}
}
closesocket(ClientSocket);
closesocket(ListenSocket);
WSACleanup();
The client (sender) is listed below.
WSADATA wsaData;
int bytes_sent;
char send_msg[] = "super cool message!";
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in ConnectInfo;
ConnectInfo.sin_family = AF_INET;
ConnectInfo.sin_addr.s_addr = inet_addr("127.0.0.1");
ConnectInfo.sin_port = htons(8888);
connect(ConnectSocket, (SOCKADDR*)&ConnectInfo, sizeof(ConnectInfo));
bytes_sent = send(ConnectSocket, send_msg, sizeof(send_msg), 0);
closesocket(ConnectSocket);
WSACleanup();
I am doing this in Windows 7 with a MinGW compiler. Thank you in advance.
You did not show your client sending code, but offhand, your server reading code is not taking into account that sends and receives are NOT 1-to-1 in TCP/IP programming. If a client sends 2 messages, the server may receive both messages, or even portions of them, in the same read operation! You ae telling your server socket to read 2048 bytes at a time. It will return whatever is currently available on the socket at that moment. TCP/IP is a byte stream, you need to treat it as much. That means you need to either put a delimiter in between your messages, or put a frame around them, in order to know where one message ends and the next message begins.

Socket program Python vs C++ (Winsock)

I have python program which works perfectly for internet chatting. But program built on similar sockets in C++ do not work over internet.
Python program
import thread
import socket
class p2p:
def __init__(self):
socket.setdefaulttimeout(50)
self.port = 3000
#Destination IP HERE
self.peerId = '59.95.18.156'
#declaring sender socket
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM )
self.socket.bind(('', self.port))
self.socket.settimeout(50)
#starting thread for reception
thread.start_new_thread(self.receiveData, ())
while 1:
data=raw_input('>')
#print 'sending...'+data
self.sendData(data)
def receiveData(self):
while 1:
data,address=self.socket.recvfrom(1024)
print data
def sendData(self,data):
self.socket.sendto(data, (self.peerId,self.port))
if __name__=='__main__':
print 'Started......'
p2p()
I want to built similar functionality in c++. I took server and client programs from MSDN. But they are working only on localhost not over internet ..
they are as follows...
Sender
#include <stdio.h>
#include "winsock2.h"
void main() {
WSADATA wsaData;
SOCKET SendSocket;
sockaddr_in RecvAddr;
int Port = 3000;
char SendBuf[3]={'a','2','\0'};
int BufLen = 3;
//---------------------------------------------
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);
//---------------------------------------------
// Create a socket for sending data
SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//---------------------------------------------
// Set up the RecvAddr structure with the IP address of
// the receiver (in this example case "123.456.789.1")
// and the specified port number.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("59.95.18.156");
//---------------------------------------------
// Send a datagram to the receiver
printf("Sending a datagram to the receiver...\n");
sendto(SendSocket,
SendBuf,
BufLen,
0,
(SOCKADDR *) &RecvAddr,
sizeof(RecvAddr));
//---------------------------------------------
// When the application is finished sending, close the socket.
printf("Finished sending. Closing socket.\n");
closesocket(SendSocket);
//---------------------------------------------
// Clean up and quit.
printf("Exiting.\n");
WSACleanup();
return;
}
Receiver
#include <stdio.h>
#include "winsock2.h"
#include<iostream>
using namespace std;
void main() {
WSADATA wsaData;
SOCKET RecvSocket;
sockaddr_in RecvAddr;
int Port = 3000;
char RecvBuf[3];
int BufLen = 3;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
//-----------------------------------------------
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);
//-----------------------------------------------
// Create a receiver socket to receive datagrams
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//-----------------------------------------------
// Bind the socket to any address and the specified port.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));
//-----------------------------------------------
// Call the recvfrom function to receive datagrams
// on the bound socket.
printf("Receiving datagrams...\n");
while(true){
recvfrom(RecvSocket,
RecvBuf,
BufLen,
0,
(SOCKADDR *)&SenderAddr,
&SenderAddrSize);
cout<<RecvBuf;
}
//-----------------------------------------------
// Close the socket when finished receiving datagrams
printf("Finished receiving. Closing socket.\n");
closesocket(RecvSocket);
//-----------------------------------------------
// Clean up and exit.
printf("Exiting.\n");
WSACleanup();
return;
}
Thank you very much for any help ..
Sorry for too much code in the question.
Per the docs, sendto returns a number that's >0 (number of bytes sent) for success, <0 for failure, and in the latter case you use WSAGetLastError for more information. So try saving the sendto result, printing it (as well as the size of the data you're trying to send), and in case of error print the last-error code too. What do you see then?