Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to compile this simple socket program [in c]?

Status
Not open for further replies.

bagyavinoth

Junior Member level 1
Joined
Dec 18, 2009
Messages
16
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,410
hi,
i am trying to create a simple tcp/ip client in c. i refer to the below weblink https://www.pcs.cnu.edu/~dgame/sockets/sockets.html which gave me a simple client.c code. but when i try to compile it using the command line tool
C:\>gcc -lnsl -lsocket -o client client.c
In file included from client.c:1:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h:533:23: sys/ty
pes.h: No such file or directory
In file included from client.c:1:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h:539: error: sy
ntax error before "off64_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdio.h:546: error: sy
ntax error before "ftello64"
client.c:3:24: sys/socket.h: No such file or directory
client.c:4:24: netinet/in.h: No such file or directory
client.c:5:19: netdb.h: No such file or directory
client.c: In function `main':
client.c:19: error: storage size of 'sin' isn't known
client.c:20: error: storage size of 'pin' isn't known
client.c:28: warning: assignment makes pointer from integer without a cast
client.c:35: error: `AF_INET' undeclared (first use in this function)
client.c:35: error: (Each undeclared identifier is reported only once
client.c:35: error: for each function it appears in.)
client.c:36: error: dereferencing pointer to incomplete type
client.c:40: error: `SOCK_STREAM' undeclared (first use in this function)

then i downloaded those header files that are missing and i put it in the gcc's include directory as follows sys/types.h,sys/socket.h,netinet/in.h,netdb.h.

after that when i try to compile it, the log is as follows
C:\>gcc -lnsl -lsocket -o client client.c
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
sys/socket.h:23,
from client.c:3:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:6:20: end
ian.h: No such file or directory
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/
sys/socket.h:23,
from client.c:3:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:11: error
: syntax error before "enum"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:88: error
: syntax error before "in_port_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:89: error
: syntax error before "in_addr_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:92: error
: syntax error before "in_addr_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:96: error
: field `imr_multiaddr' has incomplete type
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:97: error
: field `imr_interface' has incomplete type
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:101: erro
r: field `imr_multiaddr' has incomplete type
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:102: erro
r: field `imr_address' has incomplete type
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:103: erro
r: syntax error before "int32_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:107: erro
r: syntax error before "int32_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:110: erro
r: syntax error before '}' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:115: erro
r: syntax error before "sa_family_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:119: erro
r: `int16_t' undeclared here (not in a function)
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:120: erro
r: `uint16_t' undeclared here (not in a function)
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:120: erro
r: invalid application of `sizeof' to incomplete type `in_addr'
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:121: erro
r: syntax error before '}' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:177: erro
r: syntax error before "uint8_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:179: erro
r: syntax error before "u6_addr32"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:180: erro
r: syntax error before '}' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:184: erro
r: syntax error before '}' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:187: erro
r: syntax error before "uint16_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:189: erro
r: syntax error before "sin6_flowinfo"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:191: erro
r: syntax error before "sin6_scope_id"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:195: erro
r: syntax error before "sa_family_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:199: erro
r: invalid application of `sizeof' to incomplete type `sockaddr_in6'
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:200: erro
r: invalid application of `sizeof' to incomplete type `in_addr'
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:200: erro
r: size of array `sin_zero' is too large
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:201: erro
r: syntax error before '}' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:205: erro
r: field `ipv6mr_multiaddr' has incomplete type
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:207: erro
r: syntax error before "int32_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:211: erro
r: field `flr_dst' has incomplete type
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:212: erro
r: syntax error before "uint32_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:214: erro
r: syntax error before "flr_share"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:215: erro
r: syntax error before "flr_flags"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:216: erro
r: syntax error before "flr_expires"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:217: erro
r: syntax error before "flr_linger"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:218: erro
r: syntax error before "__flr_pad"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:290: erro
r: field `ipi6_addr' has incomplete type
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:291: erro
r: syntax error before "int32_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:295: erro
r: field `ifr6_addr' has incomplete type
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:296: erro
r: syntax error before "uint32_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:305: erro
r: syntax error before "uint8_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:307: erro
r: syntax error before "type"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:308: erro
r: syntax error before "segments_left"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:313: erro
r: syntax error before "uint8_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:325: erro
r: field `rt_hdr' has incomplete type
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:326: erro
r: syntax error before "uint32_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:329: erro
r: syntax error before '}' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:334: erro
r: syntax error before "uint32_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:344: erro
r: syntax error before "nexthdr"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:345: erro
r: syntax error before "hop_limit"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:349: erro
r: syntax error before '}' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:359: erro
r: syntax error before "htonl"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:359: erro
r: syntax error before "hostlong"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:360: erro
r: syntax error before "htons"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:360: erro
r: syntax error before "hostshort"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:361: erro
r: syntax error before "ntohl"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:361: erro
r: syntax error before "netlong"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:362: erro
r: syntax error before "ntohs"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:362: erro
r: syntax error before "netshort"
In file included from client.c:3:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:24:19: pr
oxy.h: No such file or directory
In file included from client.c:3:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:33: error
: syntax error before "typedef"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:37: error
: syntax error before "GObject"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:40: error
: syntax error before "port"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:41: error
: conflicting types for 'addr'
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:327: erro
r: previous declaration of 'addr' was here
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:43: error
: syntax error before "sockfd"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:44: error
: syntax error before "watchid"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:45: error
: syntax error before "thread"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:47: error
: syntax error before '*' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:48: error
: syntax error before '}' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:51: error
: syntax error before "GObjectClass"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:55: error
: syntax error before "gint"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:58: error
: syntax error before "socket_ctx_get_type"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:60: error
: syntax error before '*' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:62: error
: syntax error before "guint16"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:63: error
: syntax error before "guint32"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:64: error
: syntax error before "guint16"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:67: error
: syntax error before "socket_ctx_set_nonblock"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:67: error
: syntax error before "gboolean"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:68: error
: syntax error before "socket_ctx_set_data"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:71: error
: syntax error before "ProxyCtx"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:72: error
: syntax error before '*' token
In file included from client.c:5:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:4:23: sys/cdef
s.h: No such file or directory
In file included from client.c:5:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:18: error: syn
tax error before "struct"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:25: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:26: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:29: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:32: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:35: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:37: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:39: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:41: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:47: error: syn
tax error before "socklen_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:50: error: syn
tax error before '}' token
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:52: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:53: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:54: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:55: error: syn
tax error before "socklen_t"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:57: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:58: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:64: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:74: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:78: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:86: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:87: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:88: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:89: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:90: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:93: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:96: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:99: error: syn
tax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:102: error: sy
ntax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:114: error: sy
ntax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:115: error: sy
ntax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:116: error: sy
ntax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:117: error: sy
ntax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:118: error: sy
ntax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:120: error: sy
ntax error before "__THROW"
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netdb.h:121: error: sy
ntax error before "__THROW"
In file included from client.c:6:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/string.h:36: error: sy
ntax error before "void"
client.c: In function `main':
client.c:19: error: storage size of 'sin' isn't known
client.c:20: error: storage size of 'pin' isn't known
client.c:28: warning: assignment makes pointer from integer without a cast
client.c:35: error: `AF_INET' undeclared (first use in this function)
client.c:35: error: (Each undeclared identifier is reported only once
client.c:35: error: for each function it appears in.)
client.c:36: error: dereferencing pointer to incomplete type
client.c:40: error: `SOCK_STREAM' undeclared (first use in this function)
client.c: At top level:
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:108: erro
r: storage size of `ipi_spec_dst' isn't known
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:109: erro
r: storage size of `ipi_addr' isn't known
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:197: erro
r: storage size of `sin_addr' isn't known
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:190: erro
r: storage size of `sin6_addr' isn't known
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:200: erro
r: storage size of `sin_zero' isn't known
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:347: erro
r: storage size of `saddr' isn't known
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/netinet/in.h:348: erro
r: storage size of `daddr' isn't known
C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/sys/socket.h:41: error
: storage size of `addr' isn't known
sorry i'm very new to programming.. can anybody solve this issue. i'm using windows xp.. thanx in advance....
 

amraldo

Advanced Member level 4
Joined
Aug 29, 2004
Messages
1,185
Helped
145
Reputation
290
Reaction score
40
Trophy points
1,328
Location
Egypt
Activity points
5,880
What environment are you using?
--
Amr
 

bagyavinoth

Junior Member level 1
Joined
Dec 18, 2009
Messages
16
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,410
i tried to compile it using MINGW under windows xp.
 

amraldo

Advanced Member level 4
Joined
Aug 29, 2004
Messages
1,185
Helped
145
Reputation
290
Reaction score
40
Trophy points
1,328
Location
Egypt
Activity points
5,880
I think you need to install the sockets packages using mingw do not add headers urself.
--
Amr Ali
 

bagyavinoth

Junior Member level 1
Joined
Dec 18, 2009
Messages
16
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,410
can you tell me where shall i get this sockets packages ..
sorry for disturbing you...thanx
 

amraldo

Advanced Member level 4
Joined
Aug 29, 2004
Messages
1,185
Helped
145
Reputation
290
Reaction score
40
Trophy points
1,328
Location
Egypt
Activity points
5,880
The same way u installed mingw itself :)
--
Amr
 

microKernel

Full Member level 3
Joined
Jul 15, 2005
Messages
171
Helped
34
Reputation
68
Reaction score
12
Trophy points
1,298
Location
NE
Activity points
2,307
You are using windows XP and if you just want to learn socket programming then you may refer to winsock
Here is getting started
http://msdn.microsoft.com/en-us/library/ms738545(VS.85).aspx

and a sample client/server
https://msdn.microsoft.com/en-us/library/ms899596.aspx

Regarding your code.
The program that you are using was tested on Solaris and you'll need to make modifications to run it on other platforms. Since you are new to programming so it's better to reduce distractions initially and choose the service provided by that OS.
 

srizbf

Advanced Member level 5
Joined
Apr 3, 2010
Messages
1,995
Helped
417
Reputation
840
Reaction score
328
Trophy points
1,363
Activity points
10,985
the header files you are including are not available in windows.

you must either remove or use
#ifdef WIN32 etc.

use the following:

#ifdef WIN32
#include <windows.h>
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif

then your code will compile under mingw.

use appropriate linker libraries also.

srizbf
9thjune2010
 

bagyavinoth

Junior Member level 1
Joined
Dec 18, 2009
Messages
16
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,410
hi srizbf,
thank you for your reply. i made those changes to the code and it is as follows,
Code:
#include <stdio.h>
#include <string.h>

#ifdef WIN32
#include <windows.h>
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#endif 

#define PORT        0x1234
             /* REPLACE with your server machine name*/
#define HOST        "america3"
#define DIRSIZE     8192

main(argc, argv)
int argc; char **argv;
{
        char hostname[100];
	char    dir[DIRSIZE];
	int	sd;
	struct sockaddr_in sin;
	struct sockaddr_in pin;
	struct hostent *hp;

        strcpy(hostname,HOST);
        if (argc>2)
            { strcpy(hostname,argv[2]); }

	/* go find out about the desired host machine */
	if ((hp = gethostbyname(hostname)) == 0) {
		perror("gethostbyname");
		exit(1);
	}

	/* fill in the socket structure with host information */
	memset(&pin, 0, sizeof(pin));
	pin.sin_family = AF_INET;
	pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
	pin.sin_port = htons(PORT);

	/* grab an Internet domain socket */
	if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}

	/* connect to PORT on HOST */
	if (connect(sd,(struct sockaddr *)  &pin, sizeof(pin)) == -1) {
		perror("connect");
		exit(1);
	}

	/* send a message to the server PORT on machine HOST */
	if (send(sd, argv[1], strlen(argv[1]), 0) == -1) {
		perror("send");
		exit(1);
	}

        /* wait for a message to come back from the server */
        if (recv(sd, dir, DIRSIZE, 0) == -1) {
                perror("recv");
                exit(1);
        }

        /* spew-out the results and bail out of here! */
        printf("%s\n", dir);

	close(sd);
}

and then i tried to compile it and the log is as follows,

C:\>gcc -lnsl -lsocket -o client client.c
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot fin
d -lnsl
collect2: ld returned 1 exit status

then i just tried this
C:\>gcc -lsocket -o client client.c
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot fin
d -lsocket
collect2: ld returned 1 exit status

finally i tried this one
C:\>gcc -o client client.c
C:\DOCUME~1\sys2\LOCALS~1\Temp/ccBTKBTl.o:client.c: (.text+0x6e): undefined refer
ence to `gethostbyname@4'
C:\DOCUME~1\sys2\LOCALS~1\Temp/ccBTKBTl.o:client.c: (.text+0xde): undefined refer
ence to `htons@4'
C:\DOCUME~1\sys2\LOCALS~1\Temp/ccBTKBTl.o:client.c: (.text+0x104): undefined refe
rence to `socket@12'
C:\DOCUME~1\sys2\LOCALS~1\Temp/ccBTKBTl.o:client.c: (.text+0x150): undefined refe
rence to `connect@12'
C:\DOCUME~1\sys2\LOCALS~1\Temp/ccBTKBTl.o:client.c: (.text+0x1a6): undefined refe
rence to `send@16'
C:\DOCUME~1\sys2\LOCALS~1\Temp/ccBTKBTl.o:client.c: (.text+0x1ee): undefined refe
rence to `recv@16'
collect2: ld returned 1 exit status
sorry if i had done anything silly ... please guide me... thanx...

with regards,
vinoth.

Added after 1 minutes:

Added after 1 minutes:

hi microKernel,
now i can understand the issue to some extent. i refer to the sample client code in this link as you referred. i took a copy of it and tried to compile it using vc6.

i got the following error
--------------------Configuration: winsock - Win32 Debug--------------------
Compiling...
Socket.c
e:\software\msdev98\myprojects\winsock\socket.c(5) : fatal error C1083: Cannot open include file: 'strsafe.h': No such file or directory
Error executing cl.exe.

winsock.exe - 1 error(s), 0 warning(s)

i think what i did is proper. can you sort this issue.. thank you....

regards
vinoth.

Added after 27 minutes:
 

srizbf

Advanced Member level 5
Joined
Apr 3, 2010
Messages
1,995
Helped
417
Reputation
840
Reaction score
328
Trophy points
1,363
Activity points
10,985
from the error messages it seems that you are trying to link some unix libs.
which is not in win32.

this command line should be changed.
gcc -lnsl -lsocket -o client client.c

to:

gcc -o client.exe client.c -lws2_32

i dont know what is 'nsl'. it is not in mingw.

srizbf
9thjune2010
 

microKernel

Full Member level 3
Joined
Jul 15, 2005
Messages
171
Helped
34
Reputation
68
Reaction score
12
Trophy points
1,298
Location
NE
Activity points
2,307
Allow me to say that you are trying to do the work without understanding or even reading any of the web pages referenced. Just pulling the code and compiling doesn't work most of the time.

If you had read, the first link that you posted had code tested on Solaris.
The one I gave to you as a SAMPLE was for Win CE.

I'll again suggest try to define the problem before attaching at it.

I am attaching the code that I have compiled and tested to be fine on Win7, using Visual Studio 2010 RC.

it is from
http://msdn.microsoft.com/en-us/library/ms737591(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms737593(v=VS.85).aspx

with needed defines and link options.
 

bagyavinoth

Junior Member level 1
Joined
Dec 18, 2009
Messages
16
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,410
hi srizbf,
now i'm able to compile my code using the statement gcc -o client.exe client.c -lws2_32. thank you very much for your response......

hi microKernel,
i'll try the code which you attached and then i'll get back to you... because i don't have win7 & visual studio 2010.....thank you for your reply..

regards,
vinoth
 

microKernel

Full Member level 3
Joined
Jul 15, 2005
Messages
171
Helped
34
Reputation
68
Reaction score
12
Trophy points
1,298
Location
NE
Activity points
2,307
You don't need Win 7 and VS2010. You can reuse the code on windows XP/Vista as well. It is just that I tested them with this combination.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top