C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 29th, 2003, 02:27 AM
diwaa diwaa is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 8 diwaa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy header files

Error E2209 Server.cpp 6: Unable to open include file 'unistd.h'
Error E2209 Server.cpp 10: Unable to open include file 'sys/socket.h'
Error E2209 Server.cpp 11: Unable to open include file 'netinet/in.h'
Error E2209 Server.cpp 12: Unable to open include file 'arpa/inet.h'
Error E2209 Server.cpp 13: Unable to open include file 'sys/wait.h'

can any one send me these header files
regards

Reply With Quote
  #2  
Old June 29th, 2003, 02:32 AM
bes bes is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 13 bes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
can you show us some code?...what u are working on

Reply With Quote
  #3  
Old June 29th, 2003, 02:35 AM
diwaa diwaa is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 8 diwaa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i m trying to compile Server.cpp of Beej's Network Programming Guide.

Reply With Quote
  #4  
Old June 29th, 2003, 02:45 AM
diwaa diwaa is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 8 diwaa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
/*
** server.c - a stream socket server demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#define MYPORT 3490 // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold

struct sockaddr
{
unsigned short sa_family; // address family, AF_xxx
char sa_data[14]; // 14 bytes of protocol address
};

struct sockaddr_in
{
short int sin_family; // Address family
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8]; // Same size as struct sockaddr
};
void sigchld_handler(int s)
{
while(wait(NULL) > 0);
}

int main(void)
{
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector’s address information
int sin_size;
struct sigaction sa;
int yes=1;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}

if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
{
perror("setsockopt");
exit(1);
}

my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), ’\0’, 8); // zero the rest of the struct

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
{
perror("bind");
exit(1);
}

if (listen(sockfd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}

sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;

if (sigaction(SIGCHLD, &sa, NULL) == -1)
{
perror("sigaction");
exit(1);
}

while(1)
{ // main accept() loop

sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
perror("accept");
continue;
}

printf("server: got connection from %s\n",
inet_ntoa(their_addr.sin_addr));
if (!fork())
{ // this is the child process
close(sockfd); // child doesn’t need the listener
if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn’t need this
}
return 0;
}

Reply With Quote
  #5  
Old June 29th, 2003, 02:49 AM
bes bes is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 13 bes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
it means your compiler is missing header files...i just compiled it and i got 1 error....

c:\program files\microsoft visual studio\myprojects\hello\hello.cpp(3) : fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory
Error executing cl.exe.

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

im not sure where u can find header files....or find a different compiler

Last edited by bes : June 29th, 2003 at 02:57 AM.

Reply With Quote
  #6  
Old June 29th, 2003, 02:56 AM
diwaa diwaa is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 8 diwaa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
i m using C++ Borland 5.5 compiler

Reply With Quote
  #7  
Old June 29th, 2003, 02:59 AM
bes bes is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 13 bes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
im using Visual C++6.0 on debug level 4

Reply With Quote
  #8  
Old June 29th, 2003, 11:18 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 21 m 5 sec
Reputation Power: 997
That's cuz they're include files for *NIX systems, such as Linux or FreeBSD. Guess you didn't read the start of Beej's guide too well. You'd probably want to read these sections, especially the second one:
http://www.ecst.csuchico.edu/~beej/...ntro.html#AEN52
http://www.ecst.csuchico.edu/~beej/...ro.html#windows

Reply With Quote
  #9  
Old June 29th, 2003, 11:22 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 23 m 55 sec
Reputation Power: 462
[sound of palm of my hand slapping my forehead]
Guys, the code was written for UNIX/Linux. You're using Windows. This is why you should always indicate what OS you're using and why most first replies you get ask you for the OS.

In particular, these lines are very specific to Unix/Linux:
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

Two features in that program and that are very much tied to UNIX/Linux and which Windows does not support is forking new processes and handling signals. You might be able to emulate signal handling with Windows Messages, but creating processes in Windows appears to be very different from forking and I have no idea what kind of interprocess communication (IPC) Windows supports.

But the sockets part is directly translatable to Windows. Most of the include files (except for signal.h and wait.h) are replaced by winsock.h. The rest of the procedure, including how to create a console app in Visual C++6, is described in the PDF file, "Transitioning from UNIX to Windows Socket Programming" by Paul O'Steen at http://cs.baylor.edu/~donahoo/pract...dowsSockets.pdf . It and some working winsock code are available through the website for the book, "The Pocket Guide to TCP/IP Sockets: C Version", at http://cs.baylor.edu/~donahoo/PocketSocket/ .

You might also want to refer to the Winsock Programmer's FAQ at http://tangentsoft.net/wskfaq/ for more information about winsockand the differences between winsock programming and UNIX sockets programming.

The bottom line is that sockets programming in Windows requires using Winsock. Fortunately Winsock supports most (if not all) sockets functions, so most UNIX sockets code can be ported to Windows/Winsock with just a few changes (as described in O'Steen's PDF file). The only real problem is when the UNIX code contains features that are very UNIX-specific, like forking, pipes, signals, etc. If the UNIX code uses multithreading, then you should be able to replace it with Win32 multithreading, but the functions are different so you will need to rewrite that part of the program.

Last edited by dwise1_aol : June 29th, 2003 at 11:27 AM.

Reply With Quote
  #10  
Old July 1st, 2003, 08:21 AM
diwaa diwaa is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 8 diwaa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
now tell me the problem here
code
----------------------------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <winsock.h>

#define MYPORT 3490
#define BACKLOG 10

void sigchld_handler(int s)
{
while(wait(NULL) > 0);
}

int main(void)
{
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
struct sigaction sa;
int yes=1;

WSADATA wsaData;


if (WSAStartup(MAKEWORD(3, 0), &wsaData) != 0)
{
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}

if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1)
{
perror("setsockopt");
exit(1);
}

my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))== -1)
{
perror("bind");
exit(1);
}

if (listen(sockfd, BACKLOG) == -1)
{
perror("listen");
exit(1);
}

sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;

if (sigaction(SIGCHLD, &sa, NULL) == -1)
{
perror("sigaction");
exit(1);
}

while(1)
{
sin_size = sizeof(struct sockaddr_in);

if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
{
perror("accept");
continue;
}

printf("server: got connection from %s\n",
inet_ntoa(their_addr.sin_addr));

if (!fork())
{
close(sockfd);

if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn’t need this
}
return 0;
}

---------------------------------------------------------------------------------------------------

errors are

C:\Borland\BCC55\Bin>bcc32.exe Server.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
Server.c:

Warning W8065 Server.c 12: Call to function 'wait' with no prototype in function sigchld_handler
Warning W8057 Server.c 13: Parameter 's' is never used in function sigchld_handler
Error E2450 Server.c 35: Undefined structure 'sigaction' in function main
Error E2449 Server.c 35: Size of 'sa' is unknown or zero in function main
Error E2450 Server.c 35: Undefined structure 'sigaction' in function main
Error E2450 Server.c 35: Undefined structure 'sigaction' in function main
Error E2449 Server.c 35: Size of 'sa' is unknown or zero in function main
Error E2451 Server.c 76: Undefined symbol 'sa_handler' in function main
Error E2451 Server.c 77: Undefined symbol 'sa_mask' in function main
Warning W8065 Server.c 77: Call to function 'sigemptyset' with no prototype in function main
Error E2451 Server.c 78: Undefined symbol 'sa_flags' in function main
Error E2451 Server.c 78: Undefined symbol 'SA_RESTART' in function main
Error E2451 Server.c 80: Undefined symbol 'SIGCHLD' in function main
Warning W8065 Server.c 80: Call to function 'sigaction' with no prototype in function main
Warning W8065 Server.c 99: Call to function 'fork' with no prototype in function main

*** 10 errors in Compile ***

plz guide me

Reply With Quote
  #11  
Old July 1st, 2003, 08:38 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
This is still at least a mix of unix/linux code.
Windows does not support fork, at least as far as I know.

Reply With Quote
  #12  
Old July 1st, 2003, 09:12 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,861 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 23 m 55 sec
Reputation Power: 462
No, Windows does not support fork. Nor does it support signals. Those are both UNIX/Linux-specific.

However, you can do multithreading in Windows 95 and up (I'm not sure which version of NT started supporting it), AKA "Win32". Win32 features are also available in Win32 console applications, AKA "DOS" (not to be confused with the earlier "DOS Classic").

For the second time, the web site for the book, "The Pocket Guide to TCP/IP Sockets: C Version", at http://cs.baylor.edu/~donahoo/PocketSocket/ also has working Winsock adaptations of the book's example source code. That includes a multithreaded TCP server, TCPEchoServer-ThreadWS. You should download the Winzip archive of the source code, since each project consists of multiple source files. The page they are on, http://cs.baylor.edu/~donahoo/PocketSocket/winsock.html , even points out which of the book's UNIX programs could not be converted to Winsock because Windows does not support forking or signals.

BTW, if your server will only serve one client at a time, then there should be no need for multithreading (I didn't mention forking processes since that is not an option). Or you could handle multiple sockets by using the select() function. The above-mentioned book site also has a server that uses select(), TCPEchoServer-SelectWS.

Last edited by dwise1_aol : July 1st, 2003 at 10:00 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > header files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump