473,696 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

writing a simple server in c for windows

10 New Member
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 7141
sridhard2406
52 New Member
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
shailesh333
10 New Member
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 New Member
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
shailesh333
10 New Member
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
2944
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 form, PHP reads the .php file in a variable. It then processes it: adds the user comments to the var and writes the modified file back to disk. Next time the user opens the page (s)he sees te comments (s)he and others added. So what I have is a...
2
329
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 a Key called "TEST" under the Application in the Registry based on the microsoft article 329291 and using Solution 1 in that article In the code I basically do the following Dim MyLog as new EventLog( MyLog.Source = "TEST
385
17165
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 extraneous jargons, such as in the Unix & Perl community. Unlike mathematicians, where in mathematics there are no fewer jargons but each and every one are
3
2608
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 at certain points while executing. The monitoring app I want to write will be very simple, just picking up these traces and displaying them to show progress of the service. My question is what is the best method of communication between the...
2
1872
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 connections async. if the user pushes the selected buttons. i just want a program that connects to another computer on the port and IP address selected and if the other computer is listening, then connect to it but this code doesnt work : Imports...
6
2528
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, I'm having trouble writing to the server's event log. Here's some details: Server and workstation both in the same workgroup Logged into server as local Administrator Logged into workstation as a local user that is only in the Users group on
12
4165
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 Vista later this year. Users will quickly get used to the flashy Mac-Like UI of Vista and expect all their apps to support Vista specific features. Java will try to play catchup but but the UI will stll be primitive compared to .Net, and it...
35
4579
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 finishes. for example if in my c program I have a line: system("notepad"); I can see with the taskmanager that an instance of notepad.exe is
0
8597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9010
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8880
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8853
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4356
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4611
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2319
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.