Skip to main content

computer networks lab 6 tcp socket

 Write a program using TCP socket for wired network for following

a. Say Hello to Each other

b. File transfer.


Server Program:

/****************** SERVER CODE ****************/

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

int main(){
int welcomeSocket, newSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;


welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

serverAddr.sin_family = AF_INET;

serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr(“127.0.0.1”);
memset(serverAddr.sin_zero, ‘\0’, sizeof serverAddr.sin_zero);

bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

if(listen(welcomeSocket,5)==0)
printf(“Listening\n”);
else
printf(“Error\n”);

addr_size = sizeof serverStorage;
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);

strcpy(buffer,”Hello World\n”);
send(newSocket,buffer,13,0);

return 0;
}

Client Program:

/****************** CLIENT CODE ****************/

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

int main(){
int clientSocket;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;

clientSocket = socket(PF_INET, SOCK_STREAM, 0);

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr(“127.0.0.1”);
memset(serverAddr.sin_zero, ‘\0’, sizeof serverAddr.sin_zero);

addr_size = sizeof serverAddr;
connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

recv(clientSocket, buffer, 1024, 0);

printf(“Data received: %s”,buffer);

return 0;
}


Comments

Popular posts from this blog

FIRST YEAR SPPU NOTES

FIRST YEAR SPPU NOTES  ENGINEERING MECHANICS : Unit 1 -  OPEN PDF Unit 2 -  OPEN PDF Unit 3 -  OPEN PDF Unit 4 -  OPEN PDF ENGINEERING GRAPHICS : Unit 1 -  OPEN PDF Unit 2 -  OPEN PDF Unit 3 -  OPEN PDF Unit 4 -  OPEN PDF ENGINEERING MATHEMATICS - 2 : NIRALI PRAKASHAN UNIT 1 TO 4 -  OPEN  PDF ENGINEERING PHYSICS :                                  COMING SOON BASIC ELECTRONICS ENGINEERING :                                  COMING SOON

BACHELOR OF ENGINEERING IN ARTIFICIAL INTELLIGENCE AND DATA SCIENCE | B.E. (AI-DS)

Artificial Intelligence and Data Science is an interdisciplinary branch of science, engineering and technology creating a complete ecosystem and a paradigm shift in virtually every sector of the technical industry, academics and research. Artificial Intelligence and Data Science is the future of technology which are changing the world at very high pace. The basic objectives of this course is to train students with the next age of Intelligence and analytics generated by machines, influencing nearly every facet of our lives to help improve efficiencies and augment human capabilities, influencing consumer products with significant breakthroughs in healthcare, manufacturing, finance and retail industries. With the tremendous amount of data generated every day and the computing power available, Data Science plays important role helping every business organisation in identifying business trends and changes through advanced Big Data Analytics with variety of techniques and tools to interpret...