Menu
  • HOME
  • TAGS

How to extract data from HTTP header in C?

c,http-headers,winsock

The header ends with \r\n\r\n. If the whole response is in the receive buffer and you put a '\0' at the end of the response, then you can use the following code to find the start of the data section char *data = strstr( buffer, "\r\n\r\n" ); if ( data...

Should WSASocket() be used with IOCP?

c++,sockets,winsock,iocp,overlapped-io

I always thought that you could answer this question by looking at the MSDN docs for socket() and WSASocket() and, specifically that you couldn't create a socket that can be used with overlapped I/O (and IOCPs) using socket() as only WSASocket() allows you to specify the WSA_FLAG_OVERLAPPED flag when you...

(ProtoBuf) Can't parse message of type “data.Data” because it is missing required fields: ID

c++,protocol-buffers,winsock

In general, the "standard" way to tell between different incoming message types in protobuf is to define a union type that would contain these different messages and also a field to determine which of them is present. See official docs on this here. Example for your case: message A {...

UTF8 Byte to String & Winsock GetStream

c#,utf-8,winsock,utf8-decode

It's perfectly normal for UTF-8 encoded text to be more bytes than the number of characters. In UTF-8 some characters (for example á and ã) are encoded into two or more bytes. As the ReadFully method returns garbage if you try to use it to read more than fits in...

How to exit a blocking recv() call?

c++,sockets,winapi,network-programming,winsock

You can shutdown() the socket for input. The recv() will unblock and return zero and everybody will be happy.

Error when starting MongoDB on Windows: getnameinfo errno 10106

mongodb,winsock

The error code 10106 is about this WinSock error: WSAEPROVIDERFAILEDINIT [10106]: Service provider failed to initialize. This error is returned if either a service provider's DLL could not be loaded (LoadLibrary failed) or the provider's WSPStartup or NSPStartup function failed. To fix it, open cmd as admin, type the following...

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

c++,sockets,winsock,iocp

A completion will be queued to the IOCP associated with a socket only if an API call that can generate completions is called in a way that requests a completion to be queued. So you will know which API calls can generate completions by the fact that you've read the...

winsock recv returning 0 without shutdown from peer, different behavior from when compiling for console

c++,debugging,visual-studio-2013,windows-8.1,winsock

When this breaks recievedBytesA = 2, recievedBytesB = 2, recievedBytesC = 0, dataType = 0, dataSize = 0 You are calling ConnectSock.Recieve() when dataSize is 0. There is nothing to read, so Receive() reports that 0 bytes were read. You need to add a check for that condition: unique_ptr<char[]>...

How does mono use BSD Sockets?

c#,sockets,mono,winsock

You're slightly misunderstanding, what [MethodImplAttribute(MethodImplOptions.InternalCall)] means is that the implementation of that method is within the Mono implementation rather than within an assembly the runtime loads. It may well call into an OS function (for sockets it certainly will: why reimplement an OS service). But it does not have to...

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

c++,winsock

It is not safe, unless you can guarantee at least that your structure is a POD, which it is in your case, and that both platforms use the same endianness, which you don't know. EDIT There are some additional issues that may occur: alignment is one (the compiler may pad...

Winsock asynchronous multiple WSASend with one single buffer

asynchronous,buffer,winsock,send,iocp

I don't have a documentation reference that confirms this is possible but I've been doing it for years and it hasn't failed yet, YMMV. You CAN use a single data buffer as long as you have a unique OVERLAPPED structure per send. Since the WSABUF array is duplicated by the...

CAsyncSocket and sending/transmitting data with notifications

mfc,winsock,asyncsocket

There is no way to control how many bytes a call to Receive will give you. So you must buffer the stuff and wait for another call to OnReceive.

Winsock binding on the same port

c++,windows,winsock

You have to have 1 thread in the server dedicated to accept new connections. The process listens to new connections and assigns a new port (in the server) for that connection, making the default port available for new connections. in the server you will have N+1 socket ports open at...

Netcat like listening program in delphi does not send back commands

sockets,delphi,winsock,remote-access,netcat

This is not absolutely correct so far, because for some reason it echoes back the first command and for the rest it needs two enters to execute. program Project1; {$APPTYPE CONSOLE} uses Windows, SysUtils, WinSock, uSockFunc in 'uSockFunc.pas'; var WSAData: TWSAData; ServerSocket,ClientSocket: TSocket; ServerAddr, ClientAddr: TSockAddr; ClientAddrSize: Integer; Buffer1, buffer2...

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

c#,windows-store-apps,winsock

Yes, the bulk of the winsock APIs are available for both Windows 8.1 and Windows Phone 8.x Store Apps (and of course Universal Windows Apps in Windows 10). There are still some restrictions around access to loopback addresses etc. but the APIs should be available to you.

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

delphi,winsock

That's a variant record, like a C union. Here it is: type in_addr = record case integer of 0: (S_un_b: SunB); 1: (S_un_w: SunW); 2: (S_addr: u_long); end; So, S_addr holds the value 1745529024, and the other two members are overlaid on the same memory. In hex, 1745529024 is $680AA8C0....

Retrieving port number using winsock sockets API

c,sockets,winsock

getsockname() returns the local port number. Since your socket was not bound to a specific local port when you called connect(), a random ephemeral port got chosen, port 56179. If you want the port number you connected to, use getpeername()...

Pinvoke / call native Windows API function from C#

c#,dll,winsock,winsock2,winsockets

You must pass 2 << 8 | 2 as the fist parameter (it is the version requested from the WSA) Note that there is a small bug in the signature Microsoft produced (see Is the .NET use of WSAStartup safe for 64-bit apps?), but it isn't a problem, so you...

Error 10093 on accept() on different thread

c++,multithreading,winsock

Sockets do not have a thread affinity, so you can freely create a socket in one thread and use it in another thread. You do not need to call WSAStartup() on a per-thread basis. If accept() reports WSANOTINITIALISED then either WSAStartup() really was not called beforehand, or else WSACleanup() was...

C++ sockets: communication between PCs over internet

c++,network-programming,winsock

The main problem is, that every router is using NAT to distinguish different computer in your lokal network against the WAN. He need to do this, because you got only one IP in the internet, but several devices in your home. To archive this, he uses groups of ports. That...

UDP unconnected socket sending fails until it receives 1st datagram

sockets,networking,udp,winsock

The problem was caused by Windows Firewall. I turned off the firewall on both computers the problem goes away. Normally Windows brings up a dialog when it blocks access but one of the computers had disabled "Notify me when Windows Firewall blocks a new program" so there was no dialog...

Using an existent TCP connection to send packets

windows,tcp,winsock,socks

Are you asking how to make someone else's program send data over its existing Winsock connection? I've done exactly this but unfortunately do not have the code on-hand at the moment. If you give me an hour or two I can put up a working example using C; if you...

Sending BYTE* over socket

c++,pointers,winsock

Use reinterpret_cast to cast from BYTE* to char*. A BYTE is an unsigned char typedef, so you shouldn't have any issues. char* foo = reinterpret_cast<char*>(bar); Where bar is your BYTE*....

Does WSASend() hide batching of large buffers?

tcp,buffer,winsock

The limit is the socket send and receive buffer size respectively. WSASend() blocks while the socket send buffer is full and returns when everything has been transferred to the socket send buffer. Meanwhile, asynchronously, TCP is removing data from the socket send buffer, turning it into TCP segments in a...

Winsock2 login Form c++

c++,forms,login,winsock,send

The reason it does not work is because your MIME body data is malformed: you are not using dashes correctly for the boundaries in between each MIME part. If you get rid of (or at least reduce) the dashes in your boundary, this is easier to see. your text values...

Winsock upload file to remote server

c++,winsock

TCP is a byte stream. It has no concept of message boundaries. You are not making sure that send() is actually sending everything you give it, or that recv() is reading everything you ask it to. They CAN return fewer bytes. You have to account for that. Try this instead:...

C++ term does not evaluate to a function taking 0 arguments thread sockets

c++,multithreading,winsock

receiving_func(void* v) takes 1 argument, but std::thread(&TcpClient::receiving_func); requires a function that takes zero arguments. What do you believe v will be in the function? You could perhaps std::thread(&TcpClient::receiving_func, NULL); to compile (and set v == NULL), or since you're not using v, just remove it from the method signature. Additionally,...

select() function fails in winsock

c,select,winsock,winsock2

Simple typo: if (t = SOCKET_ERROR) should be: if (t == SOCKET_ERROR) ...

get IP address of local machine on c++

c++,winapi,ip,winsock

Your code gets adapter addresses, which are local. If you want your Internet address, you need to use the Internet, not your local network. You need to replicate the functionality of asking an external site what IP it sees you connecting from. See here for some suggestions for how to...

In C, how to malloc and free a SOCKET (already a pointer)

sockets,winapi,malloc,free,winsock

If you look at winsock.h more closely, you will see that SOCKET is not a pointer, it is an integer: typedef UINT_PTR SOCKET; UINT_PTR is not a pointer to a UINT (PUINT and LPUINT are). It is a UINT that is the same size as a pointer. So UINT_PTR is...

How to prevent empty input on a WinSock in C

c,winsock

As you do not tell us anything about the server side we may only guess: gets(buf) and "just pressing enter" leaves buf "empty". send(s,buf,strlen(buf),0); sends nothing, as buf had been left "empty". Assuming an echo-server it "echos" (sends) nothing, as nothing had been received by it, send to it from...

recv function always gives me the same buffer

c++,sockets,winsock,winsock2

while(getline(ss, tok, ',') && i < 16 ){ matrix[i] = static_cast<float>(::atof(tok.c_str())); i++ ; } Since you never reset i, matrix will never be updated after the first time. The recv is fine....

NUL values in the first character in receve function of sockets in c++ while sending data from android client

java,android,c++,sockets,winsock

int result = recv(socket, messageString, strlen(messageString), 0); should instead read int result = recv(socket, messageString, sizeof(messageString), 0); Also try printing the values of each byte of message right before socketOutputStream.write(message); to make sure that you aren't transmitting the null, ACK...

Winsock send a string

c++,string,sockets,winsock

Conversion to a char array may be what you're looking for: send(client, win.c_str(), win.size(), 0); Also as already mentioned, you should check for return values (to detect any possible error) and keep in mind that send and recv don't always send/receive all the data on the first try (you have...

recv() not receiving the number of bytes expected

c++,sockets,tcp,winsock,recv

send() returns the number of bytes transferred to the socket send buffer. If it returns 50,000, then 50,000 bytes were transferred. If you didn't receive them all, the problem is at the receiver, or in the network. You would have to post some code before any further analysis is possible....

gethostbyname and endianness - how are the bytes returned?

winapi,language-agnostic,winsock,endianness

The addresses contained in the hostent structure are in network byte order. If you have code that suggests otherwise then you are misinterpreting that code and reaching the wrong conclusion. In network byte order, on a little endian host, 127.0.0.1 is 0x0100007f. To see how this works, remember that on...

“Un-associate” socket from completion port

c++,sockets,winapi,winsock,iocp

A handle that is associated with an I/O Completion Port is removed from the port when the handle is closed. In case of a network socket, the handle is closed by calling closesocket(). The documentation for CreateIoCompletionPort contains remarks on resource handling: The handle passed in the FileHandle parameter can...

C++ Custom Client Handler

c++,sockets,winsock

I see several issues with your code. You are passing ListenSocket to Client() when you should be passing ClientSocket instead: addClient2Pool(Client(ClientSocket, inet_ntoa(n.sin_addr))); If you need to shut down the server for any reason, you should close any active client sockets first. In your example, if accept() fails, you are calling...

Winsock recv() endlessly receives 10053 WSAECONNABORTED error

c++,mfc,winsock

Okay, I believe I understand now. In my mind I was assuming that creating a socket was like memory allocation, and if you allocated memory on the client, you had to deal with it on the client, but a network socket connection can be dealt with as a 2 way...

c++ winsock - server communicates only with single client while it should communicate with every client

c++,sockets,winsock

You are only sending data to the clients whos fd is in readfd, that is, only to that one which just communicated to you. Try to test FD_ISSET(j, mainfd) instead.

recv in Visual C++ is not returning any data until newline received

c,winsock,recv

Your client is probably not sending anything until you hit enter, due to line buffering -- stdio usually buffers the input until it sees a newline (this allows you to, among other things, edit the line you're writing before sending it). If you have any control over the client, you...

Get all IP-Addresses in C

c,winsock

Here is an example I tested on on linux. I dont have access to a Windows system until tomorrow, but can test and update the answer if required. It is comparable to the Windows version only without the WSAStartup call at the beginning. #include <unistd.h> #include <stdio.h> #include <errno.h> #include...

Winsock, command line on a remote server

c++,winsock

You cannot use a socket to redirect CreateProcess() I/O. Use pipes from CreatePipe() instead. Refer to MSND for an example: Creating a Child Process with Redirected Input and Output You will have to write some monitoring code that passes data back and forth between the pipes and socket as needed....

C++ server side not blocking on listen()

c++,sockets,connection,server,winsock

First of all, let's clarify the exact semantics of the listen() and accept() functions. listen function: The listen function places a socket in a state in which it is listening for an incoming connection. Remarks: To accept connections, a socket is first created with the socket function and bound to...

Winsock use system proxy settings

winapi,proxy,winsock,wininet

There is no such thing as a "system proxy". WinInet's proxy settings are part of WinInet only, not Windows itself (Internet Explorer uses WinInet, so WinInet configurations affect IE, but not WinSock). CONNECT 127.0.0.1:8080 HTTP/1.0\r\n\r\n is a connection string for establishing a tunnel through an HTTP-based proxy server (see Tunneling...

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

multithreading,c++11,winsock

std::thread MessageThread(&cRunServer::HandleConnection, hClientSocket, &cRunServer::pConnectedClients[i]); I'm going guess that HandleConnection is a non-static member function of cRunServer and hClientSocket is not the object of cRunServer required. Add it: std::thread MessageThread(&cRunServer::HandleConnection, this, hClientSocket, &cRunServer::pConnectedClients[i]); Assuming this is a cRunServer*. See How to use boost bind with a member function how boost::function...

C# packet sent to c++ winsockets

c#,c++,sockets,marshalling,winsock

Even though I never tried such approach, I think you cannot simply share data between 2 distinct applications using winsock, then just passing pointer. Memory is protected between both. You will need to serialize your data to a memory stream. Using the proper classes with Encoding etc. for your strings....

Why do inet_ntoa and inet_ntop “reverse” the bytes?

c,networking,winsock,byte-order

Endianness is the reason. The whole point of these functions is not to produce a "readable" integer, but to set a 32-bit quantity that is ready to be shipped out on the wire. IPv4 requires big-endian ordering, so I would wager that if you did printf("%02X\n", ((char *)&IP)[0]));, you'd get...

IOCP notification without completion key

c,winsock,winsock2,iocp

You are assigning 0 as the completion key for your listening socket, and your RIO completion queues. You are assigning other completion keys for the client sockets that AcceptEx() populates when accepting clients. GetQueuedCompletionStatus() reports the completion key of the socket that was performing a queued IOCP operation. When AcceptEx()...

WSASend completion routine has never been called

c++,windows,winsock,winsock2,overlapped-io

Completion callbacks are invoked during alertable wait. You have no alertable wait, so the completion callbacks get queued up but never get a chance to run. Change WaitForMultipleObjects to a loop with WaitForMultipleObjectsEx and Sleep to SleepEx, and pass TRUE as the bAlertable parameter. This is explained right in the...

Should I use different WSAOVERLAPPED struct for WSASend and WSARecv?

windows,sockets,winsock,winsock2

If you are using event handle (a member of the WSAOVERLAPPED structure) you should definitely use two different structures for sending and receiving.

Winsock ~ Creating an UDP Listener (Multiple vs 1 socket)

c++,networking,udp,winsock,winsock2

Unlike TCP, UDP is connection-less, and as such you don't need to create separate sockets for each party. One UDP socket can handle everything. Bind it to a local IP/Port and call WSARecvFrom() once, and when it reports data to your IOCP you can process the data as needed (if...

String Length Winsock + UTF8 + “ç”

c#,ruby,utf-8,winsock

I solved this. byte[] buffer = new byte[client.TCPClient.Available]; client.TCPClient.GetStream().Read(buffer, 0, client.TCPClient.Available); Thank you....

Shortening wait time for TCP connection using Winsock2 and C

c,tcp,winsock,winsock2

You should be able to do this scanning effectively with a single thread if you employ non-blocking sockets. In order to accomplish this, you would use the ioctlsocket() function. You can roll this into your connectsock() function fairly easily. If you slip this block of code between your socket() and...

How do applications handle ports if they are not open?

c++,sockets,winsock

That completely depends on you. There is a mechanism called port knocking: The application just tries a range of ports until it finds one it can bind to. Obviously server AND client have to do this to find each other if the default port did not work out. You could...

Winsock invalid received byte number

c++,c++11,winsock

This part for (unsigned int i = receivedBytes; i < receivedBytes + m_iResult; i++) { if (m_serverMsg[i] == '\r\n') { retry = false; break; } } is not correct, '\r\n' is not the same as "\r\n", so you can not compare it like this. To correct it use strstr function...

Send numerical data via TCP/IP ethernet

c++,c,winsock,fpga

This is not a question of TCP vs UDP. First, to send a single float value, you don't reinterpret it as an address, but take the address of the float iResult = send(ConnectSocket, &f, (int) sizeof(f), 0); But this works only, if you have the same architecture on both ends...

Get IP address for all connections in Delphi

delphi,winsock

If you want to enumerate the local machine's current available IP addresses, use GetAdaptersInfo() or GetAdaptersAddresses(). If you want to enumerate the local machine's current active TCP/IP socket connections, use GetTcpTable() or GetTcpTable2() for IPv4 connections, and GetTcp6Table() or GetTcp6Table2() for IPv6 connections....

Physical disconnection from network and intermittent socket error 10057

sockets,winsock

I would expect you would get an error only if you try to send something. That is when the TCP connection would discover it can't reach the other end point. This will take a variable amount of time to discover the failure, depending on the network round trip time. There...