LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2004, 09:03 AM   #1
vibhory2j
Member
 
Registered: Apr 2004
Location: India
Posts: 42

Rep: Reputation: 15
help!! Networking Programming in C (Windows/Linux)


hello,

I am learning Networking Programming in C. I am reffering Unix Network Programming vol 1 (W. Richards Stevens).i want to run the code given in this book on windows platform, if it is possible pls let me know.

i am also not able to compile those programs on Linux (Red Hat 9.0) system on gcc , it gives some "struct..... type error".

i also want to know,a network application made on Linux/Unix enviorment will work for windows or not.
 
Old 04-14-2004, 10:43 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Network programming is very similar between UNIX and Windows. One major difference is that under Windows you have to start and stop Winsock. If you paste the code I'd be able to tell you more.
 
Old 04-15-2004, 10:00 AM   #3
vibhory2j
Member
 
Registered: Apr 2004
Location: India
Posts: 42

Original Poster
Rep: Reputation: 15
thanks, i will be in touch with you
 
Old 04-16-2004, 08:13 AM   #4
vibhory2j
Member
 
Registered: Apr 2004
Location: India
Posts: 42

Original Poster
Rep: Reputation: 15
thanks for your reply,

The code for a simple tcp server client is as follows:
#include "unp.h"

int
main(int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;

if (argc != 2)
err_quit("usage: a.out <IPaddress>");

if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13); /* daytime server */
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);

if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
err_sys("connect error");

while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
recvline[n] = 0; /* null terminate */
if (fputs(recvline, stdout) == EOF)
err_sys("fputs error");
}
if (n < 0)
err_sys("read error");

exit(0);
}
how can i compile it on windows.

cheers
 
Old 04-16-2004, 02:26 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
you need to read the README and INSTALL files in the unix network proggin tar ball. they explain how to properly compile on linux.
 
Old 07-19-2006, 05:49 AM   #6
sachitha
Member
 
Registered: Aug 2003
Location: Sri Lanka
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
Does anyone know what exactly err_sys means in the code that is quoted out???
 
Old 07-19-2006, 07:17 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You need to get the code that Stevens uses in his book, because the #include "unp.h" contains his wrapper functions, he talks about them in the book (Chapter 2 I think)

Alternatively you could rewrite the code not to use his wrapper functions, that will help you to better understand the socket interface and when you move it over to Windows the transition will probbaly be easier.

graeme.
 
Old 07-19-2006, 07:44 AM   #8
nhydra
Member
 
Registered: May 2006
Distribution: Fedora Core, FreeSpire, PC-BSD, NetBSD
Posts: 96

Rep: Reputation: 15
Hi, If you tell me email i can send you a complete server/client application example that works in Linux/BSD.
Windows has the same sockets like Linux because they both use BSD network sockets. But windows has an additional "upgrade" level, and some structs and functions has diferences because this "upgrade".
Give an email and i will send you a real working example.
From the above source code i can't see some needed include files and definitions. May be they are in unp.h but may be not.

To work with sockets in linux and windows you need this include files included:

/* OS dependent include files */
#if defined __LINUX__
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>

/* Network sockets */
#include <sys/socket.h>
#include <arpa/inet.h>

#elif defined __WINDOWS__
#include <winsock.h>
#endif

As you can see there are some include files for the linux version and a one include file for a windows version.
Some of the linux include files are not needed but i use it in my application. The required files for linux are : "sys/socket.h", "netdb.h", "arpa/inet.h".

Last edited by nhydra; 07-19-2006 at 07:48 AM.
 
Old 07-20-2006, 04:01 AM   #9
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
here are some standalone linux examples. They do not need wrappers etc and show the basics.

Quote:
Originally Posted by vibhory2j
i also want to know,a network application made on Linux/Unix enviorment will work for windows or not.
According to the other posts, it should not be difficult to write them platform independent. Just be aware that you need to recompile for (or on) the platform; simple copying of the executable will not work.

Last edited by Wim Sturkenboom; 07-20-2006 at 04:02 AM.
 
Old 02-23-2007, 09:44 AM   #10
mail2sanand
LQ Newbie
 
Registered: Feb 2007
Posts: 1

Rep: Reputation: 0
Hey thanks for the amazing site.

In the first programme explained in that tutorials, I compiled the programme successfully, but when executing the pgm I am getting the error,

"Connect : Connection refused
Server Connection Error"

Any ideas why????

My n/w connection is fine and where exactly do I have to look for the problem..>>>???
 
Old 02-23-2007, 10:20 AM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Is the server running?
Is your firewall blocking the connection?
 
Old 02-24-2007, 05:46 AM   #12
nhydra
Member
 
Registered: May 2006
Distribution: Fedora Core, FreeSpire, PC-BSD, NetBSD
Posts: 96

Rep: Reputation: 15
Check this out.
http://wiki.linuxquestions.org/wiki/...ogramming_in_C

Here is full network socket howto with examples. This code works on Linux,BSD, Solaris and .... Windoze with some changes.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
win32 windows programming , How in linux ? IDoIt Programming 10 05-26-2005 07:48 AM
Systems Programming - Linux vs Windows dipperdan Programming 8 09-30-2004 08:18 AM
linux networking programming help req shahg_shahg Programming 4 08-17-2004 07:41 AM
Linux Programming vs. Windows LinuxBlackBox Programming 7 10-28-2003 09:03 AM
Exp. Windows/Linux-Script Programmer - Need Linux Programming Mentor! ruttiger Linux - Newbie 2 10-28-2001 11:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration