0% found this document useful (0 votes)
63 views9 pages

Computer Networks Lab 6

This lab report describes a client-server application for sending messages between a client and server using sockets in C. The client code creates a socket, connects to the server, and sends messages input by the user to the server. It also starts a thread to continuously receive and print messages from the server. The server code creates a socket, binds it to a port, listens for incoming connections, accepts a connection from the client, and starts a thread to continuously receive and print messages from the client. It also prompts the user for messages to send back to the client.

Uploaded by

irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
63 views9 pages

Computer Networks Lab 6

This lab report describes a client-server application for sending messages between a client and server using sockets in C. The client code creates a socket, connects to the server, and sends messages input by the user to the server. It also starts a thread to continuously receive and print messages from the server. The server code creates a socket, binds it to a port, listens for incoming connections, accepts a connection from the client, and starts a thread to continuously receive and print messages from the client. It also prompts the user for messages to send back to the client.

Uploaded by

irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 9

Lab Report # 6

DEPARTMENT OF COMPUTER & SOFTWARE ENGINEERING


College of Electrical and Mechanical Engineering (CEME)
National University of Sciences and Technology (NUST)

Submitted by:
Zamin Raza Abbas

COMPUTER NETWORKS(3+1)
CE-38-B SEMESTER 5TH
FALL 2018

INSTRUCTOR(S)-IN-CHARGE: SIR MUHAMMAD HAROON


LAB REPORT # 6
Tasks:

Client Code:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include <pthread.h>

#include "iostream"

#include "unistd.h"
void* helloprint (void* f) {

char buffer[100];

long fd = (long) f;

while (1) {

recv(fd, buffer,100,0);

printf("Message rcvd,%s",buffer);

sleep(2);

int main()

int fd = socket(AF_INET, SOCK_STREAM,0);

if(fd == -1)

{
perror("SOcket Connection failed\n");

exit(1);

struct sockaddr_in s_addr;

s_addr.sin_family =AF_INET;

s_addr.sin_port =htons(80);

inet_aton("127.0.0.1",&s_addr.sin_addr);

if(connect(fd,(struct sockaddr *)&s_addr,sizeof(s_addr)) == -1)

perror("Connect failed!\n");

exit(1);

char buffer[100];

pthread_t th;

long value = 4;

pthread_create(&th, NULL, helloprint,(void*) fd);

while(1)

{
//scanf("%s",&buffer);

printf("Client plz Enter msg:");

fgets(buffer,100,stdin);

send(fd, buffer, strlen(buffer),0);

return 0;

Server Code:

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "pthread.h"

#include "iostream"

#include "unistd.h"
void* helloprint ( void* info) {

char buffer[100];

long connfd = (long) info;

while (1) {

recv(connfd, buffer,100,0);

printf("msg rcvd,%s", buffer);

sleep(2);

int main()

int fd = socket(AF_INET, SOCK_STREAM, 0);

if(fd == -1)

perror("Socket Creation Failed\n");

exit(1);

struct sockaddr_in addr;


addr.sin_addr.s_addr = INADDR_ANY;

addr.sin_family = AF_INET;

addr.sin_port = htons(80);

if(bind(fd,(struct sockaddr *)&addr,sizeof(addr))==-1)

perror("BInd failed on socket\n");

exit(1);

int backlog = 10;

if(listen(fd, backlog) == -1)

perror("Listening failed on socket\n");

exit(1);

int connfd;

struct sockaddr_in cliaddr;

socklen_t cliaddr_len = sizeof(cliaddr);

connfd = accept(fd, (struct sockaddr*)&cliaddr, &cliaddr_len);

if(connfd == -1)

{
perror("Accept failed on socket\n");

exit(1);

char buffer[100];

pthread_t th;

long value = 4;

pthread_create(&th, NULL, helloprint,(void*) connfd );

while(1)

//recv(connfd, buffer,100,0);

printf("msg rcvd,%s", buffer);

//printf("Server port number:,%d", cliaddr.sin_port);

//printf("SErver IP:,%s", inet_ntoa(cliaddr.sin_addr));

printf("\Enter Server Message:\n");

//scanf("%s",&buffer);

fgets(buffer,100,stdin)

send(connfd, buffer, strlen(buffer),0);

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy