KEMBAR78
Socket - Exceuted Ubantu | PDF | Computing | Computer Programming
0% found this document useful (0 votes)
40 views9 pages

Socket - Exceuted Ubantu

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views9 pages

Socket - Exceuted Ubantu

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

#include <stdio.

h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

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

int sockfd, portno, n;

struct sockaddr_in serv_addr;

char servermessage[1024];

if (argc < 3) {

printf("Provide required command-line arguments as <filename>


<server_IP_address> <portno>\n");

exit(1);

portno = atoi(argv[2]);
// Create socket

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0) {

perror("ERROR in opening socket");

exit(1);

// Setup server address

serv_addr.sin_family = AF_INET;

serv_addr.sin_addr.s_addr = inet_addr(argv[1]);

serv_addr.sin_port = htons(portno);

// Connect to the server

if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {

perror("ERROR in connecting");

close(sockfd);

exit(1);

// Communication loop

while (1) {

bzero(servermessage, 0, 1024); // Clear buffer


// Read input from user

fgets(servermessage, 1024, stdin);

// Send data to the server

n = write(sockfd, servermessage, strlen(servermessage));

if (n < 0) {

perror("ERROR in writing");

close(sockfd);

exit(1);

// Detect end of communication

if (strncmp("bye", servermessage, 3) == 0) {

break;

// Read response from server

bzero(servermessage, 0, 1024); // Clear buffer

n = read(sockfd, servermessage, 1024);

if (n < 0) {

perror("ERROR in reading");
close(sockfd);

exit(1);

// Print server response

printf("Server: %s\n", servermessage);

// Close socket

close(sockfd);

return 0;

//server

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <stdlib.h>

#include <sys/types.h>

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

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

int sockfd, newsockfd, portno, n;

struct sockaddr_in serv_addr, client_addr;

char servermessage[1024];

socklen_t len;

if (argc < 2) {

printf("Provide required command-line arguments as <filename> <portno>\


n");

exit(1);

portno = atoi(argv[1]);

// Create socket

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0) {

perror("ERROR in opening socket");

exit(1);

}
// Setup server address

serv_addr.sin_family = AF_INET;

serv_addr.sin_addr.s_addr = INADDR_ANY;

serv_addr.sin_port = htons(portno);

// Bind socket to port

if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {

perror("ERROR in binding");

close(sockfd);

exit(1);

} else {

printf("Binding is successful\n");

// Listen for incoming connections

listen(sockfd, 5);

// Accept connection

len = sizeof(client_addr);

newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &len);

//here & is address of len to modify the len with actual size of client address
if (newsockfd < 0) {

perror("ERROR in accepting request");

close(sockfd);

exit(1);

// Communication loop

while (1) {

bzero(servermessage, 0, 1024); // Clear buffer can use bzero () instead

memset

n = read(newsockfd, servermessage, 1024);

if (n < 0) {

perror("ERROR in reading");

close(newsockfd);

close(sockfd);

exit(1);

// Print client request

printf("Client: %s\n", servermessage);

bzero(servermessage, 0, 1024); // Clear buffer


// Read data from stdin and send to client

fgets(servermessage, 1024, stdin);// function in C is used to read a line of text

n = write(newsockfd, servermessage, strlen(servermessage));

if (n < 0) {

perror("ERROR in writing");

close(newsockfd);

close(sockfd);

exit(1);

// Detect end of communication // compare a specified number of


characters

if (strncmp("bye", servermessage, 3) == 0) {

break;

// Close sockets

close(newsockfd);

close(sockfd);

return 0;
}

You might also like