Professional Documents
Culture Documents
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#define PORT 8080
int main(){
int opt = 1;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[20] = {}, username[] = "ajay", password[] = "123";
char uname[10], pword[10];
printf("Creating socket\n");
int socket_ds = socket(AF_INET, SOCK_STREAM, 0);
if(socket_ds < 0) {
printf("\nError creating socket");
exit(1);
}
address.sin_family = AF_INET;
address.sin_port = htons(PORT);
address.sin_addr.s_addr = INADDR_ANY;
printf("Binding socket\n");
if(bind(socket_ds, (struct sockaddr*)&address, sizeof(address)) < 0){
printf("\nBind failed");
exit(1);
}
printf("Listening...\n");
if(listen(socket_ds, 3) < 0){
printf("\nListen failed");
exit(1);
}
int i;
int len = strlen(buffer);
for(i = 0 ; i < len/2; ++i){
char temp = buffer[i];
buffer[i] = buffer[len - i - 1];
buffer[len - i - 1] = temp;
}
send(new_sock, buffer, strlen(buffer), 0);
close(new_sock);
shutdown(socket_ds, SHUT_RDWR);
return 0;
}
Client
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#define PORT 8080
int main(){
int opt = 1;
char hello[20] = "good morning\0";
printf("Creating socket\n");
if(socket_ds < 0) {
printf("\nError creating socket");
exit(1);
}
address.sin_family = AF_INET;
address.sin_port = htons(PORT);
printf("Connecting to server\n");
if(connect(socket_ds, (struct sockaddr*)&address, sizeof(address)) < 0){
printf("\nConnection failed");
exit(1);
}
shutdown(socket_ds, SHUT_RDWR);
close(socket_ds);
return 0;
}