QUESTION 1)
This part is concerned with network programming in C based upon the
UNIX sockets interface.
----------------
/* sender.c */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#define DATA "My first datagram . . ."
/* usage: sender hostname portnumber */
main(argc, argv)
int argc;
char *argv[];
{
int sock;
struct sockaddr_in name;
struct hostent *hp, *gethostbyname();
/* Create socket on which to send. */
sock = socket(AF_INET,SOCK_DGRAM, 0);
if (sock == -1) {
perror("opening datagram socket");
exit(1);
}
/* Construct name, with no wildcards, of the socket to ``send'' to. */
hp = gethostbyname(argv[1]);
if (hp == (struct hostent *) 0) {
fprintf(stderr, "%s: unknown host\n", argv[1]);
exit(2);
}
memcpy((char *) &name.sin_addr, (char *) hp->h_addr, hp->h_length);
name.sin_family = AF_INET;
name.sin_port = htons( atoi( argv[2] ));
/* Send message. */
if (sendto(sock,DATA, sizeof DATA ,0,(struct sockaddr *)
&name,sizeof name) == -1)
perror("sending datagram message");
close(sock);
exit(0);
}
----------------
/* receiver.c */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
main()
{
int sock, length;
struct sockaddr_in name;
char buf[1024];
/* Create socket from which to read. */
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
perror("opening datagram socket");
exit(1);
}
/* Create name with wildcards. */
name.sin_family = AF_INET;
name.sin_addr.s_addr = INADDR_ANY;
name.sin_port = 0;
if (bind(sock,(struct sockaddr *)&name, sizeof name) == -1) {
perror("binding datagram socket");
exit(1);
}
/* Find assigned port value and print it out. */
length = sizeof(name);
if (getsockname(sock,(struct sockaddr *) &name, &length) == -1) {
perror("getting socket name");
exit(1);
}
printf("Socket port #%d\n", ntohs( name.sin_port));
/* Read from the socket. */
if ( read(sock, buf, 1024) == -1 )
perror("receiving datagram packet");
printf("-->%s\n", buf);
close(sock);
exit(0);
}
----------------
A) Do the above programs create Unix domain sockets?
B) What protocol do you think is being used for communication of the
message between sender and receiver?
C) How has this protocol been specified please?
D) Does a connection get established between sender and receiver?
E) Does the program variable name point to information about a local socket
in the program sender.c?
F) in the program receiver.c?
G) What small change do you think should be made to each program if
one decided to reserve port 3000 for communication of the message?
H) What small change should be made to receiver.c if one wanted it to
continue to read and print messages, rather than to terminate after a
single message?
I) With what system call should read() be replaced, if functionality
is to be added to receiver.c that requires the return-address of the
message?
QUESTION 2)
This part is also concerned with network programming, but i would like
you to write all your programs in Java.
A new service enables users to exchange 5 lines of text per session.
Two users at a time may access the service anonymously. Each writes a
line and, when both lines have been written, they are exchanged. Then
the users each write a second line, and so on. While this is going on,
other users will be unable to begin a session.
A) can you please write a server program for me that provides this
service at port 2347 on some machine.
B) can you write a client program for me that can be used to access the service.
C) can you provide a sample of output from two clients involved in the
same session.
D) can you write a proxy program (capable of handling at most one
client at a time) that relays lines of text between client and server.
E) can you rewrite the server program using multithreading so that it
can handle up to three simultaneous sessions.
F) can you rewrite the proxy program using multithreading so that it
is also capable of handling multiple clients at the same time.
thank you for your helping.
by |