question about using sockets on sysVr3
Patrick J. Spinler
pspinler at att1.mankato.msus.edu
Wed Jul 11 14:52:48 AEST 1990
I am attempting to learn to use sockets, however, I am evidently
creating a protocal error of some sort, and have no real idea where to
start looking. If some kind soul would please tell me why I'm getting
an EINVAL error on my call to bind(), I would be very grateful. I'm
running sysVr3.1.1 on an at&t 3b2/500 with the WINS tcp/ip and
wollengang socket emulation library installed.
Please email replys, and thanks very much !
-- Pat
--- cut here ---
#include <errno.h> /* obligatory includes */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/inet.h>
#include <sys/in.h>
#include <sys/time.h>
#include <netdb.h>
#include <string.h>
#define MAXHOSTNAME 32
#define PORTNUM 4594
main ()
{
int sock_fds, pid, fds, size;
struct sockaddr_in sock_add;
struct sockaddr_in connect_add;
struct hostent *host;
short portnum;
char hostname[MAXHOSTNAME+1], buffer[80];
bzero (&sock_add, sizeof (sock_add));
gethostname (hostname, MAXHOSTNAME);
host = gethostbyname (hostname);
if (!host) {
perror ("getbyhostname");
exit (1);
}
if (host->h_addrtype != AF_INET) {
perror ("host in wrong address family");
exit (1);
}
/*
* create the socket !
*/
sock_fds = socket (AF_INET, SOCK_STREAM, 0);
if (sock_fds < 0) {
perror ("socket");
exit (1);
}
pid = fork ();
if (pid < 0) {
perror ("fork");
close (sock_fds);
exit (1);
}
if (pid) { /* Parent process -- server */
/*
* Name the socket
*/
bzero (&sock_add, sizeof (sock_add));
sock_add.sin_family = AF_INET;
bcopy (host->h_addr, &sock_add.sin_addr, host->h_length);
sock_add.sin_port = htons ((unsigned short) PORTNUM);
if (bind (sock_fds, &sock_add, sizeof (sock_add))) {
perror ("bind");
close (sock_fds);
exit (1);
}
size = sizeof (sock_add);
if (getsockname (sock_fds, &sock_add, &size)) {
perror ("getsockname");
close (sock_fds);
exit (1);
}
sprintf (buffer, "Port #%d\n", ntohs (sock_add.sin_port));
write (1, buffer, strlen (buffer));
listen (sock_fds, 1);
size = sizeof (connect_add);
fds = accept (sock_fds, &connect_add, &size);
if (fds < 0) {
perror ("accept");
close (sock_fds);
exit (1);
}
while ((size = read (fds, buffer, 79))) {
write (1, buffer, size);
}
close (fds);
close (sock_fds);
exit (0);
}
else { /* child */
/*
* Name the socket
*/
bzero (&sock_add, sizeof (sock_add));
sock_add.sin_family = AF_INET;
bcopy (host->h_addr, &sock_add.sin_addr, host->h_length);
sock_add.sin_port = htons ((unsigned short) PORTNUM);
if (connect (sock_fds, &sock_add, sizeof (sock_add)) < 0) {
perror ("connect");
close (sock_fds);
exit (1);
}
sprintf (buffer, "connected to port #%d\n", ntohs (sock_add.sin_port));
write (1, buffer, strlen (buffer));
strcpy (buffer, "Hi, this is a test !");
size = strlen (buffer);
write (sock_fds, buffer, size);
close (sock_fds);
exit (0);
}
}
----
Patrick J. Spinler PSPinler at att1.Mankato.MSUS.EDU
Student Programmer PSPinler at vax1.Mankato.MSUS.EDU
Mankato State University PSPinler at msus1.MSUS.EDU
More information about the Comp.unix.wizards
mailing list