Commit 490aada8 authored by Donjan Rodic's avatar Donjan Rodic
Browse files

Merge branch 'master' of gitlab.ethz.ch:hpcse_hs16/lecture

parents 4a0f4ad6 e4d4dc41
CC=gcc
all: server client
server: server.c
$(CC) -o server server.c
#-lnsl
client: client.c
$(CC) -o client client.c
#-lnsl
clean:
rm -f server client
A very simple TCP client and server example, available
along with a short tutorial at:
http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html
To run it:
terminal one: ./server 5000
terminal two: ./client localhost 5000
Panos
/* client.c Creates Internet stream client for a Unix platform.
The name and port number of the server are passed in as arguments.
To compile on solaris gcc ... -lnsl -lsocket
*/
/* Source + short tutorial: http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> /* for atoi */
#include <string.h>
char *msg = "Hello from the client";
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sock, n;
unsigned short port;
struct sockaddr_in server;
struct hostent *hp;
char buffer[1024];
if (argc != 3) {
printf("Usage: %s server port\n", argv[0]);
exit(1);
}
sock= socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) error("Opening socket");
server.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp==NULL) error("Unknown host");
memcpy((char *)&server.sin_addr,(char *)hp->h_addr,
hp->h_length);
port = (unsigned short)atoi(argv[2]);
server.sin_port = htons(port);
if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0)
error("Connecting");
n = send(sock, msg, strlen(msg),0);
if (n < strlen(msg))
error("Writing to socket");
n = recv(sock, buffer, 1023,0);
if (n < 1) error("reading from socket");
buffer[n]='\0';
printf("The message from the server is %s\n",buffer);
if (close(sock) < 0) error("closing");
printf("Client terminating\n");
return 0;
}
/* server.c - creates a connection oriented (stream) Internet server
for the Unix Operating system. Port is passed in as an argument
To compile on solaris gcc ... -lnsl -lsocket */
/* Source + short tutorial: http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#define BUFSIZE 4096
extern int errno;
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sock, newsock, len, n;
unsigned int fromlen;
unsigned short port;
struct sockaddr_in server, from;
char buffer[BUFSIZE];
char *msg = "I Got your message";
if (argc < 2) {
fprintf(stderr,"usage %s portnumber\n",argv[0]);
exit(0);
}
port = (unsigned short) atoi(argv[1]);
sock=socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) error("Opening socket");
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(port);
len=sizeof(server);
if (bind(sock, (struct sockaddr *)&server, len) < 0)
error("binding socket");
fromlen=sizeof(from);
if (listen(sock,5) < 0)
error("listening");
while (1) {
newsock=accept(sock, (struct sockaddr *)&from, &fromlen);
if (newsock < 0) error("Accepting");
printf("A connection has been accepted from %s\n",
inet_ntoa((struct in_addr)from.sin_addr));
n = recv(newsock,buffer,BUFSIZE-1,0);
printf("Read %d bytes from client\n",n);
if (n < 1) {
error("Reading");
}
else {
buffer[n]='\0';
printf("Message from client is: %s\n",buffer);
len = strlen(msg);
n = send(newsock,msg,len,0);
if (n < len) error("Error writing");
if (close(newsock) < 0) error("closing");
}
}
return 0; // we never get here
}
CC=gcc
all: example3
example3: example3.c
$(CC) -o example3 example3.c
clean:
rm -f example3
# examples_mpi/example3.cpp using TCP/IP sockets :-)
How to run:
terminal one: ./example3 0 localhost 5000
terminal two: ./example3 1 localhost 5000
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
/* Source + short tutorial: http://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html */
extern int errno;
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
if (argc < 3) {
fprintf(stderr,"usage: %s [0|1] server portnumber\n",argv[0]);
exit(0);
}
int rank = atoi(argv[1]); // 0: server (rank 0), 1: client (rank 1)
/* Prepare communication */
int sock;
if (rank == 0)
{
int srv_sock;
unsigned short port;
struct sockaddr_in server, from;
int len, n;
unsigned int fromlen;
port = (unsigned short) atoi(argv[3]);
srv_sock=socket(AF_INET, SOCK_STREAM, 0);
if (srv_sock < 0) error("Opening socket");
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(port);
len=sizeof(server);
if (bind(srv_sock, (struct sockaddr *)&server, len) < 0)
error("binding socket");
fromlen=sizeof(from);
if (listen(srv_sock,5) < 0)
error("listening");
sock=accept(srv_sock, (struct sockaddr *)&from, &fromlen);
if (sock < 0) error("Accepting");
printf("A connection has been accepted from %s\n", inet_ntoa((struct in_addr)from.sin_addr));
}
else
{
unsigned short port;
struct sockaddr_in server;
struct hostent *hp;
char buffer[1024];
sock= socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) error("Opening socket");
server.sin_family = AF_INET;
hp = gethostbyname(argv[2]);
if (hp==NULL) error("Unknown host");
memcpy((char *)&server.sin_addr,(char *)hp->h_addr, hp->h_length);
port = (unsigned short)atoi(argv[3]);
server.sin_port = htons(port);
if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0)
error("Connecting");
}
/* Main code */
if (rank == 0)
{
int x = 33;
printf("Server: sending x = %d\n", x);
int n = send(sock, &x, sizeof(int), 0);
if (n < sizeof(int)) error("Error writing");
}
else
{
int y = -1;
int n = recv(sock, &y, sizeof(int),0);
if (n < sizeof(int)) error("reading from socket");
printf("Client: received y = %d\n", y);
unlink("rank1_output.txt");
FILE *fp = fopen("rank1_output.txt", "w");
fprintf(fp, "Client: received y = %d\n", y);
fclose(fp);
}
/* Finalize communication */
if (close(sock) < 0) error("closing");
return 0;
}
if [ "$#" -ne 1 ]; then
echo "usage: runme.sh port"
exit 1
fi
./example3 0 localhost $1 &
sleep 1
./example3 1 localhost $1
if [ "$#" -ne 1 ]; then
echo "usage: runme.sh port"
exit 1
fi
# you will need to adjust it - the simplest solution is to
# put the example3 executable in your $HOME directory.
./example3 0 falcon $1 &
sleep 1
ssh panda "cd example3; ./example3 1 falcon $1"
No preview for this file type
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment