Server I-Network Programming-Lab Report, Exercises of Network Programming

This lab report is for Network Programming course. It was submitted to Prof. Tausiq Dasgupta at Babasaheb Bhimrao Ambedkar University. Its main points are: Include, Server, Socket, String, Time, Line, Size, Backlog, Struct, Perror, Exit

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

dhanush
dhanush 🇮🇳

4

(3)

36 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
/*
** server.c - a stream socket server demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#define MYPORT 13 // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue
will hold
#define MAX_LINE_SIZE 256 // Maximum Characters on each Line in File
int main(int argc, char* argv[] )
{
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;
time_t ticks;
char buff[MAX_LINE_SIZE];
char yes=1;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
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");
docsity.com
pf2

Partial preview of the text

Download Server I-Network Programming-Lab Report and more Exercises Network Programming in PDF only on Docsity!

** server.c - a stream socket server demo */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <errno.h> #include <time.h>

#define MYPORT 13 // the port users will be connecting to #define BACKLOG 10 // how many pending connections queue will hold #define MAX_LINE_SIZE 256 // Maximum Characters on each Line in File

int main(int argc, char* argv[] ) { 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; time_t ticks; char buff[MAX_LINE_SIZE];

char yes=1;

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); 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");

docsity.com

exit(1); }

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

while(1) { sin_size = sizeof(struct sockaddr_in); fprintf(stderr, "Waiting for client to connect"); 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));

ticks= time(NULL); snprintf(buff,sizeof(buff), "%.24s\r\n",ctime(&ticks)); write(new_fd, buff, strlen(buff)); close(new_fd);

}

getchar(); return 0; }

docsity.com