473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

writing a simple server in c for windows

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <Winsock2.h>
  6. #include <stdlib.h>
  7.  
  8. #define PORTNUM 2343
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     char msg[] = "Hello World !\n";
  13.  
  14.     struct sockaddr_in dest; /* socket info about the machine connecting to us */
  15.     struct sockaddr_in serv; /* socket info about our server */
  16.     int mysocket;            /* socket used to listen for incoming connections */
  17.     int socksize = sizeof(struct sockaddr_in);
  18.  
  19.     memset(&serv, 0, sizeof(serv));    /* zero the struct before filling the fields */
  20.     serv.sin_family = AF_INET;         /* set the type of connection to TCP/IP */
  21.     serv.sin_addr.s_addr = INADDR_ANY; /* set our address to any interface */
  22.     serv.sin_port = htons(PORTNUM);    /* set the server port number */    
  23.  
  24.     mysocket = socket(AF_INET, SOCK_STREAM, 0);
  25.  
  26.     /* bind serv information to mysocket */
  27.     bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
  28.  
  29.     /* start listening, allowing a queue of up to 1 pending connection */
  30.     listen(mysocket, 1);
  31.     int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
  32.  
  33.     while(consocket)
  34.     {
  35.  
  36.  
  37.         printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
  38.         send(consocket, msg, strlen(msg), 0);
  39.  
  40.  
  41.     }
  42.     close(consocket);
  43.     close(mysocket);
  44.     return EXIT_SUCCESS;
  45. }

When i try to compile i get the following error:
[Linker error] undefined reference to `htons@4'
[Linker error] undefined reference to `socket@12'
[Linker error] undefined reference to `bind@12'
[Linker error] undefined reference to `listen@8'
[Linker error] undefined reference to `accept@12'

How do i get rid of this error.
Any help will be appreciated . Thanks in advance.

I have installed dev c++ on my windows machine, and have tried to compile the code.
Dec 24 '09 #1
4 7126
Hi,

Many function libraries and API's require you to link in their library files; Winsock2 is one such API. To do so, you need to add the appropriate instruction to your project. I abandoned the Dev-C++ IDE years ago, but I still use the compiler regularly by invoking it from the command line.

Winsock2 requires the library, libws2_32.a . gcc's command-line option for specifying a library file to be linked in is a lower-case "L" preceded by a minus sign. The "lib" prefix on the library file name is dropped, as is the ".a" file extension. The command-line invocation would be something like:
g++ filename.cpp -lws2_32

Somewhere in the IDE is a dialog box where you tell it what object and library files to link in.
Dec 24 '09 #2
Hi
Thanks for the reply.
I checked the compile options in dev c++ and looked for the libraries... it includes the folder dev/lib which has all the ".a" files including libws2_32.a.
So I guess the issue you mentioned may not be the cause for the problem.
Dec 24 '09 #3
Airslash
221 100+
you need to link to the ws2_32.lib file located in your system or system32 folder.
Also, prior to calling any kind of winsock function you need to initialize the library in Windows.

you need the following headers:
Expand|Select|Wrap|Line Numbers
  1. #include <winsock2.h>
  2. #include <ws2tcpip.h>
  3. #include <stdio.h>
  4.  
Initialization is done by the following code:

Expand|Select|Wrap|Line Numbers
  1. // Prepare 
  2. WSADATA wsaData;
  3. int iResult;
  4.  
  5. // Initialize Winsock
  6. // load version 2.0
  7. iResult = WSAStartup(MAKEWORD(2,0), &wsaData);
  8. if (iResult != 0) {
  9.     printf("WSAStartup failed: %d\n", iResult);
  10.     return 1;
  11. }
  12.  
After finishing your program's routines; you need to call this function to terminate the library
Expand|Select|Wrap|Line Numbers
  1. WSACleanup();
  2.  
You should visit this site, it has nice tutorials and the complete reference for the winsock2 library and API calls
http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
Dec 24 '09 #4
Thanks guys,
i fixed the issue.
To compile the code i needed to link winsock32 lin.
I used the option "-lwsock32" , (without quotes) in the compile options.
Dec 25 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Marc | last post by:
Hello, I have a question about how PHP handles multiple file reads/ writes. I made a page containing a self-submitting form where the user can type his name, topic and a text. When he submits the...
2
by: Paul P | last post by:
I am having a problem writing errors to the application log on a Windows 2003 server running IIS 6 The error message is "InvalidOperationException: Cannot open log for source {0}." I have created...
385
by: Xah Lee | last post by:
Jargons of Info Tech industry (A Love of Jargons) Xah Lee, 2002 Feb People in the computing field like to spur the use of spurious jargons. The less educated they are, the more they like...
3
by: Chris Dunaway | last post by:
I am writing a Windows Service that reads and processes files on a set schedule. I want to create a second app that can monitor the Windows service. The Windows service will write trace messages...
2
by: iwdu15 | last post by:
hey, i was wondering if 1) anyone could tell me whats wrong with my code, i did a little fixing to the msdn version fo this, or 2) how to make a simple program that will connect and listen for...
6
by: rekaeps | last post by:
We are developing an ASP.NET 2.0 (C#) application, and I'm having troubles sending e-mail from the server when accessing the web site from a separate client computer. Also, in the same scenario,...
12
by: Von Clubusev | last post by:
I like Java just as much as I like C#, but the truth is that unless you absolutely need platform portability, DotNet is the way to go. This is becoming even more true with the release of Windows...
35
by: jleslie48 | last post by:
I've written a cgi program in C using the borland 5.5 free compiler, and it runs just fine on an Apache server. My only issue is if I issue some system calls the cgi suspends until the call...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.