Linux and Windows socket APIs - c

I want to port some Linux-specific code to Windows which contains socket operations. Is there any third party library available for Linux/BSD sockets to Winsock mapping like the pthread-w32 library, which is used for the threading operation? Also, I can't use the Cygwin tool chain.

Well, I'm still working on this, but it's a start for new programs: https://sourceforge.net/projects/libxc/

Related

Partially Porting PJLIB - Without IOQUEUE, select abstraction, and socket abstraction API

I would like to use the PJSIP library to implement a small SIP softphone on an embedded system. Since this embedded system does not offer Linux or support POSIX, I would like to port the PJLIB library only partially, as described here (https://www.pjsip.org/porting.htm#mozTocId30930). The threading function can be deactivated via a macro, but I'm not quite sure yet how I have to set up this new transport function or where exactly it has to be included so that I can also bypass the IOQUEUE implementation and the PJLIB socket abstraction.
On my embedded system (Keil RTX) I can allocate a UDP socket and register a callback which is called on a network event. I also have a send function which I can use to send data packets. Although I have already looked into the stack, I can't find a way to get started.
Has anyone already dared to the partial porting and can give me a brief assistance. Thank you !
See how Symbian port worked (I think it might be removed from recent versions, but it should be still downloadable) - it was also based on non-POSIX sockets. Create your own platform-specific socket file and ioqueue file.

how to use zerotier in socket programming?

I am trying to create a file sharing application in which socket programming is done through c language and GUI is done using java. I am connecting c and java using JNI(java native interface).
Now to install this appication in different systems and establish communication between these application I was thinking of using zerotier, but I am not sure how to use zerotier to do this work of file sharing.
ZeroTier provides a BSD-style socket layer via the SDK (libzt).
Documentation / Examples: docs.zerotier.com/sockets
Github Repo: zerotier/libzt.
Basically you'd just build the library into your application and call the special ZT sockets in the same way you'd call normal sockets.

connection between windows and Linux sockets in c

I am using the socket module in python to send commands to my raspberry pi to turn GPIO pins on and of.
I am switching to C, where I will use winsock.h and winsock2.h to create the server on my PC and sys/socket.h to create a client on the raspberry pi.
Is it possible to establish a connection between these two different libraries?
I only want to create a socket, bind, send and recv. No other operations.
I recommend you to check this documentation, there are some examples for a Windows Server / Client connection:
https://learn.microsoft.com/en-us/windows/win32/winsock/getting-started-with-winsock
For Linux you need to do some adaptations as you might know or you have already implemented, I did the same for 2 desktop applications to send data from a Linux PC (client) to a Windows PC (server). As mentioned in the comments it doesn't matter the devices while they are in the same network and follow the TCP/IP protocol.
I was able to do this even connected through a VPN. Unfortunatelly I can not share the code. But I developed this communication based on the documentation from the link above.
I hope it helps. Actually if you want to use Python in the raspberry Pi there is also a python built-in package that you could use: https://docs.python.org/3/library/socket.html
And you can use the code from the link above in Windows. It should be straighforward.

Basic TCP client on Linux, OSX and Windows

I want to make a TCP client that works on Windows, Linux and osx (most important) in C. The code that I've found on SO might work on linux but not on osx and vice versa. So what do I need to make sure that it works on all three?
Thanks!
Assuming you are planning to write your code to use the BSD sockets API, you'll find that most TCP client code that works on Linux will work on MacOS/X with little or no modification, and vice versa.
Getting the code to work under Windows as well is a little bit trickier, as required #includes are different, and there are a number of cases where Windows' TCP stack (aka WinSock) behaves a little bit differently than that the TCP stacks of the other two OS's. That said, Windows does support most of the BSD sockets API, and with a bit of #ifdef-ing you can come up with a program that will compile and run correctly on all three OS's. You'll need to test and debug on all three OS's, of course; never assume that just because something works on one OS that it will work everywhere.
Depending on your program's particular needs (and your interests), it may well make sense to follow Duck's advice and find a networking library that has already done the above-described work for you; but if you prefer to "roll your own", that is doable too. A good approach when writing to the BSD sockets API is: whenever you find a piece of code that you have to write differently for different OS's, hide the implementations of that code snippet inside a function together (with an #ifdef so that the right code gets compiled under each OS), so that the rest of your program doesn't have to remember how to deal with that unpleasant detail anymore. Do that enough times and you'll end up the proud maintainer of your own cross-platform networking library ;)
I'd recommend getting your program working under Linux and/or OS/X first, and once you're happy with it, then porting it over to Windows. Some "gotchas" to watch out for when porting your network code to Windows:
Under Windows, you #include windows.h or winsock2.h to get the network definitions you need. (If you want the newer WinSock2 API, you have to include winsock2.h, and always do it before any #include of windows.h, or you'll get the wrong API version... it's a real circus)
Under Windows you have to call WSAStartup() before doing any networking stuff (if you forget, all your networking calls will error out)
Under MacOS/X and Linux, file descriptors and sockets are largely interchangeable (i.e. you can select() on STDIN_FILENO, etc). Under Windows, they are not.
Under MacOS/X and Linux, you can find out why a call failed by checking errno. Under Windows, you call WSAGetLastError() instead.
Under MacOS/X and Linux, you destroy a socket with close(). Under Windows you do it with closesocket().
Under MacOS/X and Linux, you can (if you choose) call read() and write() on your TCP socket to receive/send data (respectively). Under Windows, that won't work, you must call send() and recv() only. (send() and recv() will work under MacOS/X and Linux too)
To set a socket to non-blocking mode under Windows, you have to call ioctlsocket(fd, FIONBIO, &mode). Under MacOS/X and Linux, you call instead fcntl(fd, F_SETFL, flags).
More fun Windows-networking gotchas can be read at the WinSock Lame List.

Windows C socket programming for UDP client

I am trying to lookup some example programs for windows socket. Particularly, I am interested in writing a client in C (in visual studio) which communicates to the server using UDP. I din't find any concrete material. I tried some examples but got some linking errors. Is there any library available. Please let me know.
Thanks in advance.
The Apache Portable Runtime supports sockets, and it is cross platform.
I've found a simple library, which provides implementation of networking, but it is for C++. (C++ Socket Class for Windows). You can look at it's implementation of working with sockets, or just use it (there are examples of simple client and server).

Resources