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

Sem 8 Practical Exam

 DC LAB ASSIGNMENTS  https://github.com/siddharthlanke/Sem-VIII-Academic-Labs/tree/main LAB-1 LAB-2 LAB-3 LAB-4 https://colab.research.google.com/drive/1wf96qi9RH3oGN039QbcmU6s3PEY6txn7?usp=sharing LAB-5 https://colab.research.google.com/drive/1i_a5owxrYdRB1ZQPa6vWGsKdC0ALiSHU?usp=sharing LAB-6 https://colab.research.google.com/drive/1CdPxCss_ppyZf4rCdiEl12CQSarvTBh9?usp=sharing CI LAB ASSIGNMENTS  LAB-1 https://colab.research.google.com/drive/1s07YgacC2nTC-vQUxtT3dKifgKZBwCaI?usp=sharing LAB-2 https://colab.research.google.com/drive/1wj6x-eunLDBx3J8Q-nheShHTMftyvgDu?usp=sharing LAB-3 https://colab.research.google.com/drive/1gw4ZekLF5WYVg3r4Rn0gnwKg177rkMmo?usp=sharing LAB-4 https://colab.research.google.com/drive/1WO0bPhM_y3xSMrNLJBjvVQ7gNpGJHCys?usp=sharing DL LAB ASSIGNMENTS  LAB-1 LAB-2 https://colab.research.google.com/drive/1324qVAvwPeqWoOQgEZ7SHRXwCVQVVZs1?usp=sharing LAB-3 https://colab.research.google.com/drive/1RCX1xvUXex6ksUtQtnj5zpoOGM_fipbr?usp=sharing LAB-4 https://colab.

Sem 7 Practical Exam

 ML LAB ASSIGNMENTS  LAB-1 https://colab.research.google.com/drive/1iQXKgk2nsysWXnO7xbajrSjGcYlReIhE LAB-2 https://colab.research.google.com/drive/1Ilmd52oXNojLpIUKNpVGkDiweL3smrvz LAB-3 https://colab.research.google.com/drive/1U6tD9lT7ktMxfDFVQRXTGGMh7sSA_-Sf LAB-4 https://colab.research.google.com/drive/1S3LFQdfJ0sodYIrNBIvCbuineHN5xRBs LAB-5 https://colab.research.google.com/drive/1u61o7jXxmqw4gnvF-YBKHElMDjHDwBSN LAB-6 https://colab.research.google.com/drive/1-LdxSoE2oKJr9Q84qWPH4Xo5uM7aDvLB QAI LAB ASSIGNMENTS  LAB-1 https://colab.research.google.com/drive/1w8j9LEK_ihcgc6QYhR-s_WQ0FZLbcF3t LAB-2 https://colab.research.google.com/drive/1l10kBvC7qNS2L17f3VBmpyDpNFlG2Mj7 LAB-3 https://colab.research.google.com/drive/1V2u0BZP63lZT9rMYRTcs88XnMNK0Uz6z LAB-4 https://colab.research.google.com/drive/1i7NJukPd-TQ5VAJP02TEOKCArEylmoGp LAB-6 https://colab.research.google.com/drive/1EvbH1DuDC06dkHCJi4XUd6iG87w3DhAl DMV LAB ASSIGNMENTS  LAB-1 https://colab.research.google.com/drive/1YHv4aatqmi