FAQ Database Discussion Community

How to exit a blocking recv() call?

Based on this question, when you call closesocket() on a socket that recv() is already using, then an RST packet will be sent to the other side instead of performing a graceful disconnection (4-way handshake). But I wish to perform a graceful disconnection, so I need to exit recv() before...

Retrieving port number using winsock sockets API

Although I do get a non-zero value for port number from the code segment below, the value returned for port does not match the value for port used to establish the socket: #include <winsock2.h> int main(void) { SOCKADDR_IN server; WSADATA wsa; SOCKET s; DWORD dwTime = 1000; if (WSAStartup(MAKEWORD(2,2),&wsa) !=...

Winsock binding on the same port

So I recently decided to dabble into winsock and network programming a bit by using a guide I found and searching the net, but I have run into a problem which I am not really sure how i should solve. I'm trying to make a very simple chat system, I've...

WSASend completion routine has never been called

I'm playing around with Overlapped IO and suddenly found out that it looks like I'm the only one who can't encourage Completion callback to work (All claims was about: it works and I don't like it). The idea of my application is: a client (telnet localhost 27015) connects to the...

recv function always gives me the same buffer

This question is kind of a follow-up of a question that I previously asked. Basically I had a c++ server running in ubuntu and I'm now trying to make this code works in windows. I am having some issues with windows socket as it's my first time using them. After...

Is legacy winsock available for windows store apps? [closed]

I know that code on windows store which used socket needed to use the StreamSocket class instead of the legacy winsock API (Socket class) And it is clearly something that prevent me from migrating my open source library whose lots of app would depends. Now, I've seen that VS 2013...

Does WSASend() hide batching of large buffers?

I'm testing large buffers right now and I am setting it in WSABUF and calling WSASend() next. Thing though, is that WSARecv() just handed it back that large buffer in one go. Does that make sense? Where is the limit of WSASend() and WSARecv() with with respect to large buffers?...

Passing member function which takes another member function and var to a std::thread

I've searched for solutions, but after trying about 10 approaches I cannot solve my compilation issue: functional(1149): error C2064: term does not evaluate to a function taking 2 arguments Here's the part of my code. pConnectedClients.push_back(new RemoteClient(hClientSocket, RemoteClientNumber)); if (pConnectedClients.size() > 1) { for (int unsigned i = 0; i...

Netcat like listening program in delphi does not send back commands

I have a delphi program that listens to port 8080 using winsock here is the code so far: program Project1; {$APPTYPE CONSOLE} uses Windows, SysUtils, WinSock; var WSAData: TWSAData; ServerSocket,ClientSocket: TSocket; ServerAddr, ClientAddr: TSockAddr; ClientAddrSize,Status,ret: Integer; Buffer1,buffer2: array[0..1024] of Char; begin writeln('started..'); WSAStartup($0101,WSAData); ServerSocket:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); ServerAddr.sin_family:=AF_INET; ServerAddr.sin_port:=htons(8080); ServerAddr.sin_addr.S_addr:=INADDR_ANY;...

How to extract data from HTTP header in C?

Today I am asking how to extract the data section from the received buffer in my recv() in C (not C++). I just need some suggestions, how would I get HTTP/1.1 200 OK\r\n Date: Mon, 23 May 2005 22:38:34 GMT\r\n Server: Apache/ (Unix) (Red-Hat/Linux)\r\n Last-Modified: Wed, 08 Jan 2003 23:11:55...

Delphi winsock why does sockaddr_in values get “garbled” when assigning InAddr.S_addr

I found a function to test if a specific tcp port is open on a remote PC. It appears to work alright, but when I assign a value to the variable client.sin_addr.S_addr the other values appear to be assigned with garbled values, well at least I didn't expect to see...

Pinvoke / call native Windows API function from C#

from within my C#-application I use functions of an external DLL. This DLL itself uses network functions. So it is necessary to initialise Winsock out of my C#-application to let the network sockets work for this DLL. That's how I try to execute WSAStartup for initialisation, but it does not...

String Length Winsock + UTF8 + “ç”

Actually I have a problem with "receive data" in server. Everything works fine , here's what the server does with the data it receives: string data = null; byte[] buffer = new byte[40000]; client.TCPClient.GetStream().Read(buffer, 0, 40000); data = Encoding.UTF8.GetString(buffer); Server.Network.ReceiveData.SelectPacket(client.Index, data); Void: public static void SelectPacket(int Index, string data) {...

Is it unsafe to send and receive raw structures through sockets?

I'm working on a remoting control program. I need to send and receive multiple data at once. I use this solution: struct PACKET { int x; int y; }; //... PACKET p; p.x = 10; p.y = 5; send(socket, (char*)&p, sizeof(PACKET), 0); However, I'm considering if this is a safe...

Get all IP-Addresses in C

I have the following problem. I have to write a Plugin for Pidgin in the Language C. I am completely new to C. I found the following Code. WORD wVersionRequested; WSADATA wsaData; char name[255]; char* ip; PHOSTENT hostinfo; wVersionRequested = MAKEWORD( 2, 0 ); if ( WSAStartup( wVersionRequested, &wsaData )...

Get IP address for all connections in Delphi

The following code returns the IP address of the first TCP/IP connection for a machine. uses WinSock; // ... function GetLocalIP() : String; var addr: TSockAddrIn; phe: PHostEnt; szHostName: array[0..128] of Char; socketData: TWSADATA; begin Result := ''; // Initialize the socket API if (WSAStartup($101, socketData) = 0) then begin...

What IO operations causes completion packets to be sent to the completion port when using sockets?

In IOCP, when starting an IO operation such as WSARecv(), a completion packet will be sent to the completion port when the IO operation completes. What I want to know is what IO operations causes completion packets to be sent to the completion port when using sockets, for example, I...

Winsock invalid received byte number

I'm currently facing a Winsock related problem when trying to validate the number of received bytes. In my application I'm using a non-blocking socket in order to throw my own Timeout exception. Here the according code for the initialization of Winsock: // Initialize Winsock WSAData winSockData; WORD dllVersion = MAKEWORD(2,...

C++ sockets: communication between PCs over internet

I'm writing a program on Windows using winsocks that can send messages to another computer. The client connects with the server in the other computer and begin exchanging data. It works fine on my local network using local addresses(192.168.1.*), but I can't communicate with public addresses (; not even my...

Winsock asynchronous multiple WSASend with one single buffer

MSDN states "For a Winsock application, once the WSASend function is called, the system owns these buffers and the application may not access them." In a server application, does that mean that if I want to broadcast a message to multiple clients I cannot use a single buffer that holds...

gethostbyname and endianness - how are the bytes returned?

On my (Intel) x86 machine, I've noticed that if I printf the results of gethostbyname for localhost, I get 100007F, even though the MSDN documentation states it should return the IP in network byte order, aka big endian. I searched a bit and found this topic. Based on the answers...

Should I use different WSAOVERLAPPED struct for WSASend and WSARecv?

I'm developing a server-client application using WinSock. Does using the same WSAOVERLAPPED with both WSASend and WSARecv works well? Should I use different WSAOVERLAPPED struct for WSASend and WSARecv?...

Why do inet_ntoa and inet_ntop “reverse” the bytes?

This is a rather basic problem with which, to my surprise, I've had a problem today. It looks to me like inet_pton and inet_ntoa are reversing the bytes of the IP address they're given: DWORD IP; inet_pton(AF_INET, "", &IP); printf("%08X\n", IP); This will print 0100A8C0. And well, if we break...

“Un-associate” socket from completion port

CreateIoCompletionPort() is used to associate a socket with a completion port. However, when this socket is closed, then I need to "un-associate" it from the completion port. How can I do that?

IOCP notification without completion key

I am building an IOCP/RIO Winsock server and have been struggling with getting the proper notification from the completion port when my AcceptEx() calls are triggered by a client request. When I call GetQueuedCompletionStatus() after having sent a client request, I will get a successful return but without the completion...

How do applications handle ports if they are not open?

I am trying to write an application in c++ using winsock. I need to handle a case where default port used by the app is not open to use then what is fallback mechanism?

UTF8 Byte to String & Winsock GetStream

Well, I'm trying to convert a large information in bytes for string. (11076 length) The problem in the end, the information is with missing characters. (length 10996) Look: The information is received by Winsock connection, look the proccess: public static void UpdateClient(UserConnection client) { string data = null; Decoder utf8Decoder...

How to prevent empty input on a WinSock in C

I have a WinSock client script in C that, when i just press enter without any input, hangs up. sry i cant tell you more, i tried to read the buffer after the input but not even this is working. Thats my code: printf("\ninput [max 256]: "); gets(buf); send(s,buf,strlen(buf),0); rc=recv(s,buf,256,0);...

Should WSASocket() be used with IOCP?

I know that it is recommended to use WSAAccept() instead of accept() when creating an IOCP application. But I am not sure if WSASocket() belongs to the Overlapped I/O functions, or is it just another Winsock function?

Physical disconnection from network and intermittent socket error 10057

A customer of mine has a Windows application where there is a network connection between two machines. The system is supposed to cope with the connection being lost. It does this by keeping a counter on the client position which is reset every time data is received from the server....