0% found this document useful (0 votes)
46 views

Exp-7 NTWKLAB

This document contains code for a client and server program that communicate over a socket connection. The client code opens a socket, connects to the server, reads any data received from the server and prints it out. The server code opens a socket, binds it to port 2569, listens for incoming connections and accepts a connection. It gets the current time, formats it into a string and writes this string to the connected client before closing the connection. This repeats to service new connection requests.

Uploaded by

Supriyo Kundu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Exp-7 NTWKLAB

This document contains code for a client and server program that communicate over a socket connection. The client code opens a socket, connects to the server, reads any data received from the server and prints it out. The server code opens a socket, binds it to port 2569, listens for incoming connections and accepts a connection. It gets the current time, formats it into a string and writes this string to the connected client before closing the connection. This repeats to service new connection requests.

Uploaded by

Supriyo Kundu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

client

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#include<stdlib.h>

#include<string.h>

int main(int argc, char **argv) {


int sockfd,n=0;
char recvline[1000+1];
struct sockaddr_in servaddr;

if(argc!=2) {
printf("Error with arguments!!!");
exit(0);
}
if( (sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) {
printf("Socket Error!!!");
exit(0);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(2569);
if(connect(sockfd,(struct sockaddr *) &servaddr,sizeof(servaddr))<0) {
printf("Connect Error!!!");
exit(0);
}
else {
while( (n=read(sockfd,recvline,1000))>0) {
recvline[n] = 0; /* null terminate */
if(fputs(recvline,stdout) == EOF) {
printf("fputs error!!!");
exit(0);
}
}
}
if(n<0) {
printf("read error!!!");
exit(0);
}
exit(0);
}
Server

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#include<stdlib.h>

#include<string.h>

int main(int argc,char **argv)


{
int listenfd,connfd;
struct sockaddr_in servaddr;
char buff[1000];
time_t ticks;
listenfd = socket(AF_INET,SOCK_STREAM,0);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(2569);

bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

listen(listenfd,8);
for( ; ; ) {
connfd = accept(listenfd,(struct sockaddr *)NULL,NULL);

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

You might also like