C programming, sockets/client-server.

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
I don't even know where to start with this exercise lol.

I have to create a client-server messaging program.

I've included the winsock.h header file and have got this statement: socket(PF_INET, SOCK_STREAM, 0), can't work out how to declare a socket though I've missed so much. I'd have thought it would be something like "socket sock; sock = socket(PF_INET, SOCK_STREAM, 0);" but the compiler doesn't seem to like that. Any suggestions?
 
I don't even know where to start with this exercise lol.

I have to create a client-server messaging program.

I've included the winsock.h header file and have got this statement: socket(PF_INET, SOCK_STREAM, 0), can't work out how to declare a socket though I've missed so much. I'd have thought it would be something like "socket sock; sock = socket(PF_INET, SOCK_STREAM, 0);" but the compiler doesn't seem to like that. Any suggestions?

int mysocket;

if((mysocket = socket(PF_INET, SOCK_STREAM,0))==-1)
// throw some error

That should IIRC (but again I only do network programming on *nix
 
it should be SOCKET sock, not a pointer. SOCKET should be all capitals. All a SOCKET type is, is an unsigned int. Its different to a FILE * which is actually a pointer to a FILE structure (FILE is just a synonym for a _IO_FILE struct).
 
Last edited:
Still can't get the most basic program to work lol.

This,
Code:
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>

int main(int argc, char * argv[]) {
	SOCKET sock;
	sock = socket(PF_INET, SOCK_STREAM, 0);
	system("pause");
	return 0;
}


keeps giving me the error message.

Code:
1>Main.obj : error LNK2019: unresolved external symbol _socket@12 referenced in function _main
1>C:\Users\Administrator\Documents\Visual Studio 2008\Projects\Chat\Debug\Chat.exe : fatal error LNK1120: 1 unresolved externals
 
The error you're getting tells you that there's a problem linking, and that the linker doesn't know where to find the definition of the socket() function.

According to the almighty Google, it's in the ws2_32.lib library, which you'll need to tell the linker to use. In the project properties in Visual Studio, there should be some linker options with something like "additional dependencies" (I don't have it installed right now so don't know the actual words) which is just a whitespace-separated list of libraries -- add ws2_32.lib to that, rebuild and it should get further.
 
Just to expand on the above really (it always takes me ages to find the correct section :p):

In Visual Studio (I'm using 2008, the rest should be similar)

  1. Go to project > properties
  2. Expand Configuration Properties
  3. Expand Linker
  4. Click Input
  5. Add 'ws2_32.lib' (no quotes) to 'Additional Dependencies'

and it'll compile :).
 
Just to expand on the above really (it always takes me ages to find the correct section :p):

In Visual Studio (I'm using 2008, the rest should be similar)

  1. Go to project > properties
  2. Expand Configuration Properties
  3. Expand Linker
  4. Click Input
  5. Add 'ws2_32.lib' (no quotes) to 'Additional Dependencies'

and it'll compile :).


Thanks you two, finally got it to compile. :) Java is so much easier...
 
Back
Top Bottom