Mail Archives: cygwin/1997/01/06/14:11:31
This is a multi-part message in MIME format.
--------------105670BB3182
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
The bug below is for B17.1 of the gnu-win32
--------------105670BB3182
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Message-ID: <32D149E1 DOT 57B7 AT mci DOT com>
Date: Mon, 06 Jan 1997 13:52:17 -0500
From: "Ajay M. Desai" <Ajay DOT Desai AT mci DOT com>
Reply-To: Ajay DOT Desai AT mci DOT com
Organization: MCI Metro
X-Mailer: Mozilla 3.0Gold (WinNT; I)
MIME-Version: 1.0
To: gnu-win32 AT cygnus DOT com
Subject: Bug in socket fork code?
Content-Type: multipart/mixed; boundary="------------700C43FF47C7"
This is a multi-part message in MIME format.
--------------700C43FF47C7
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
I sent a bug report about the socket code not working after a fork.
The problem is that the WSAStartup winsock call is not always call for
all the net.c.
checkinit() is not called by socket read and wirte and thus WSAStartup
is never called in the forked child. I was able to get the child to
work by adding a unsed call to gethostbyname in to the child code.
since gethostbyname calls checkinit().
--
|-----------------------------------------------------|
| Ajay M. Desai |
| Software Engineering Consultant MCI Metro |
| Home: mailto:adesai AT erol DOT com Vienna, VA |
| Work: mailto:Ajay DOT Desai AT mci DOT com 703 918-0845 |
| MCI Mail ID: 214-0881 |
|-----------------------------------------------------|
--------------700C43FF47C7
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Message-ID: <32C84F59 DOT 6686 AT mci DOT com>
Date: Mon, 30 Dec 1996 18:25:13 -0500
From: "Ajay M. Desai" <Ajay DOT Desai AT mci DOT com>
Reply-To: Ajay DOT Desai AT mci DOT com
Organization: MCI Metro
X-Mailer: Mozilla 3.0Gold (WinNT; I)
MIME-Version: 1.0
To: gnu-win32 AT cygnus DOT com
Subject: Bug in socket fork code?
Content-Type: multipart/mixed; boundary="------------23CB2AD741CF"
This is a multi-part message in MIME format.
--------------23CB2AD741CF
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
The following code compiles but hangs. If the server fork is removed
it works just fine.
--
|-----------------------------------------------------|
| Ajay M. Desai |
| Software Engineering Consultant MCI Metro |
| Home: mailto:adesai AT erol DOT com Vienna, VA |
| Work: mailto:Ajay DOT Desai AT mci DOT com 703 918-0845 |
| MCI Mail ID: 214-0881 |
|-----------------------------------------------------|
--------------23CB2AD741CF
Content-Type: text/plain; charset=us-ascii; name="server.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="server.c"
/*
* Connects to port 1234 on the local host.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define NSTRS 3 /* no. of strings */
/*
* Strings we send to the client.
*/
char *strs[NSTRS] = {
"This is the first string from the server.\n",
"This is the second string from the server.\n",
"This is the third string from the server.\n"
};
extern int errno;
int main()
{
char c;
FILE *fp;
int fromlen;
char hostname[64];
struct hostent *hp;
register int i, s, ns;
struct sockaddr_in sin, fsin;
/*
* Before we can do anything, we need
* to know our hostname.
*/
gethostname(hostname, sizeof(hostname));
/*
* Now we look up our host to get
* its network number.
*/
if ((hp = gethostbyname(hostname)) == NULL) {
fprintf(stderr, "%s: host unknown.\n", hostname);
exit(1);
}
/*
* Get a socket to work with. This socket will
* be in the Internet domain, and will be a
* stream socket.
*/
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("server: socket");
exit(1);
}
/*
* Create the address that we will be binding to.
* We use port 1234 but put it into network
* byte order. Also, we use bcopy (see
* Chapter 14) to copy the network number.
*/
sin.sin_family = AF_INET;
sin.sin_port = htons(1234);
bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
/*
* Try to bind the address to the socket.
*/
if (bind(s, (struct sock_addr*)&sin, sizeof(sin)) < 0) {
perror("server: bind");
exit(1);
}
/*
* Listen on the socket.
*/
if (listen(s, 5) < 0) {
perror("server: listen");
exit(1);
}
while(1){
/*
* Accept connections. When we accept one, ns
* will be connected to the client. fsin will
* contain the address of the client.
*/
if ((ns = accept(s, (struct sock_addr*)&fsin, &fromlen)) < 0) {
perror("server: accept");
exit(1);
}
if (fork() == 0){
/*
* We'll use stdio for reading the socket.
*/
fp = fdopen(ns, "r");
/*
* First we send some strings to the client.
*/
for (i = 0; i < NSTRS; i++)
send(ns, strs[i], strlen(strs[i]), 0);
/*
* Then we read some strings from the client
* and print them out.
*/
for (i = 0; i < NSTRS; i++) {
while ((c = fgetc(fp)) != EOF) {
putchar(c);
if (c == '\n')
break;
}
}
/*
* We can simply use close() to terminate the
* connection, since we're done with both sides.
*/
close(ns);
exit(0);
}
else{
close(ns); // parent
}
}
return(0);
}
--------------23CB2AD741CF
Content-Type: text/plain; charset=us-ascii; name="client.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="client.c"
/*
* Connects to the local host at port 1234.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#define NSTRS 3 /* no. of strings */
/*
* Strings we send to the server.
*/
char *strs[NSTRS] = {
"This is the first string from the client.\n",
"This is the second string from the client.\n",
"This is the third string from the client.\n"
};
extern int errno;
main()
{
char c;
FILE *fp;
char hostname[64];
register int i, s;
struct hostent *hp;
struct sockaddr_in sin;
/*
* Before we can do anything, we need to know
* our hostname.
*/
gethostname(hostname, sizeof(hostname));
/*
* Next, we need to look up the network
* address of our host.
*/
if ((hp = gethostbyname(hostname)) == NULL) {
fprintf(stderr, "%s: unknown host.\n", hostname);
exit(1);
}
/*
* Get a socket to work with. This socket will
* be in the Internet domain, and will be a
* stream socket.
*/
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("client: socket");
exit(1);
}
/*
* Create the address we will be connecting to.
* We use port 1234 but put it into network
* byte order. Also, we use bcopy (see Chapter
* 14) to copy the network number.
*/
sin.sin_family = AF_INET;
sin.sin_port = htons(1234);
bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
/*
* Try to connect to the address. For this to
* succeed, the server must already have bound
* this address, and must have issued a listen()
* request.
*/
if (connect(s, (struct sock_addr*)&sin, sizeof(sin)) < 0) {
perror("client: connect");
exit(1);
}
/*
* We'll use stdio for reading
* the socket.
*/
fp = fdopen(s, "r");
/*
* First we read some strings from the server
* and print them out.
*/
for (i = 0; i < NSTRS; i++) {
while ((c = fgetc(fp)) != EOF) {
putchar(c);
if (c == '\n')
break;
}
}
/*
* Now we send some strings to the server.
*/
for (i = 0; i < NSTRS; i++)
send(s, strs[i], strlen(strs[i]), 0);
/*
* We can simply use close() to terminate the
* connection, since we're done with both sides.
*/
close(s);
exit(0);
}
--------------23CB2AD741CF
Content-Type: text/plain; charset=us-ascii; name="Makefile"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Makefile"
all: server.exe client.exe
server.exe: server.c
gcc -g -o $@ $<
client.exe: client.c
gcc -g -o $@ $<
--------------23CB2AD741CF--
--------------700C43FF47C7--
--------------105670BB3182--
-
For help on using this list, send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".
- Raw text -