Skip to content
Advertisement

C Server/Client with Sockets

[Just 2 minor questions on the ground of this remaining]

I try to write a simple server/client in C to send messages over sockets. It must run under Linux and Windows with MinGW. I found many examples for Linux but way to many arent working with Windows. It would be really nice if you would help me.

For the server I have something I dont understand.

What I’m doing on server-side?

  1. Need to initialise WSA on Windows, nothing on Linux.
  2. Create a socket for the server on server-side.
  3. Create struct sockaddr_in for the server.
  4. Bind the socket on ANY-IP.
  5. Listen on the socket.
  6. Accept connections, handle connection with the newSocket.
  7. Close the new Socket repeat with 6).

It is only working correctly if I dont close the newSocket, but why? (EBADF error)

Code Update after Edit2:

JavaScript

What I’m doing on client-side?

  1. Need to initialise WSA on Windows, nothing on Linux.
  2. Create a socket for the client on client-side.
  3. Create struct sockaddr_in for the server.

[try 1]

  1. Create struct sockaddr_in for the client.
  2. Bind the socket to the struct for client.
  3. Connect with client Socket to server-struct.
  4. Send a message.

[try 2]

  1. Use sendto because I only want to send one message.

Both isnt working, I think my problem is the struct sockaddr_in, but I havent any idea right know why. What I’m doing wrong here?

View Edit 3 for solutions.

JavaScript

Edit 1

Commenting includes out: I dont have any compiler errors, just a warning because int received is not in use. I comment that out because I tried a lot and wanted to clean it up before I post it here, but thought it could be important enough to keep it as a comment. Maybe its included in another include? I will check that.

I test and write on windows right now, but finally it needs to run on linux too. I test the server above with a small tool in Autoit on windows on the same machine which connects to a server and do a GET request. The server got the GET, printed it in his console and send a reply back which the Autoit-client got and printed, so it worked once. Without the close Operation I can do it everytime.

Edit 2 – got answer for the server, client still not running

Server is running fine now, got the answer from: http://cs.baylor.edu/~donahoo/practical/CSockets/WindowsSockets.pdf

Moving from UNIX sockets to Windows sockets is fairly simple. Windows programs require a different set of include files, need initialization and deallocation of WinSock resources, use closesocket( ) instead of close( ), and use a different error reporting facility. However, the meat of the application is identical to UNIX.

Edit 3 – client working, but one minor question

Need to shorten the links because I’m not allowed to post that many links directly.

My mistake in try 1 was to bind the client struct to the same IP as the server. “127.0.0.1”(clientaddress) => “Pseudo” and its working.

JavaScript

But I dont need to bind here by myself, connect does it if its not done, and it handles that its an unused address. pubs.opengroup [.] org/onlinepubs/9699919799/functions/connect.html

If the socket has not already been bound to a local address, connect() shall bind it to an address which, unless the socket’s address family is AF_UNIX, is an unused local address.

JavaScript

Of course this is working, but in my opinion not correct.

JavaScript

This here should also work in my opinion, I shouldnt need connect in this structure because it should be built in sendto.

pubs.opengroup [.] org/onlinepubs/9699919799/functions/connect.html

The connect() function shall attempt to make a connection on a connection-mode socket[…]

pubs.opengroup [.] org/onlinepubs/9699919799/functions/sendto.html

If the socket is connection-mode, dest_addr shall be ignored.

Because of the text above I think this should also work, but it doesnt. Maybe someone can say why? (Or maybe it should work without myAddress and bind)

JavaScript

And btw the return value for send and sendto isnt clearly.

Successful completion of a call to send() does not guarantee delivery of the message. A return value of -1 indicates only locally-detected errors.

Successful completion of a call to sendto() does not guarantee delivery of the message. A return value of -1 indicates only locally-detected errors.

I think the return value is useless, or not? If its -1 it can be delivered, if 1 it maybe isnt. Maybe determine another protocol?

minor questions

  1. Why I still need connect for sendto?
  2. Can I have a clear return value from send / sendto with another protocol?

Will search for both and edit it here if I found an answer, will still watch if someone can answer this. My major-problems are gone so really thanks to all.


Thanks for reading!

Advertisement

Answer

It won’t work because you never call connect(). You should check return value from calls like connect() , send () etc.
The solution : You should call connect after you created the socket.

JavaScript

To use send or sendTo with TCP, the socket has be connected or you will get an error.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement