Sunteți pe pagina 1din 6

CPE 400

Mei Yang
Socket Programming
Client Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>


#define PORT_NUM 42000 // Port number used by server
#define IP_ADDR "127.0.0.1" // IP address of server

int main()
{
int client_s; // Client socket descriptor
struct sockaddr_in server_addr; // Server Internet address
char out_buffer[4096]; // Output buffer for data
char in_buffer[4096]; // Input buffer for data
int retcode; // Return code
int pal_flag; //palindrome flag
int i; //counter


// Create client socket
// - AF_INET is Address Family Internet and SOCK_STREAM is streams
client_s = socket(AF_INET, SOCK_STREAM, 0);
if (client_s < 0)
{
printf("*** ERROR - socket() failed \n");
exit(-1);
}


// Fill-in the server's address information and do a connect with the
// listening server using the client socket - the connect() will block.
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUM); // Port num to use
server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use
retcode = connect(client_s, (struct sockaddr *)&server_addr,
sizeof(server_addr));
if (retcode < 0)
{
printf("*** ERROR - connect() failed \n");
exit(-1);
}

// Receive from the server using the client socket
retcode = recv(client_s, in_buffer, sizeof(in_buffer), 0);
if (retcode < 0)
{
printf("*** ERROR - recv() failed \n");
exit(-1);
}

while(strcmp(in_buffer,"exit") != 0)
{
// Output the received message
if(strcmp(in_buffer,"exit") == 0)
{
break;
}
// Print message from server
printf("Received from server: %s \n", in_buffer);
if(strcmp(in_buffer,"exit") == 0)
{
break;
}
pal_flag = 1;

// Check if palindrome
for(i=0;i<((int)strlen(in_buffer)/2);i++)
{
if(in_buffer[i] != in_buffer[((int)strlen(in_buffer)-1-i)])
{
pal_flag = 0;
break;
}
}

// Send that message is a palindrome to server
if(pal_flag == 1)
{
strcpy(out_buffer, "This is a palindrome");
retcode = send(client_s, out_buffer, (strlen(out_buffer) + 1), 0);
if (retcode < 0)
{
printf("*** ERROR - send() failed \n");
exit(-1);
}
}
// Send that message is not a palindrome to server
else
{
strcpy(out_buffer, "This is not palindrome");
retcode = send(client_s, out_buffer, (strlen(out_buffer) + 1), 0);
if (retcode < 0)
{
printf("*** ERROR - send() failed \n");
exit(-1);
}
}

// Receive from the server using the client socket
retcode = recv(client_s, in_buffer, sizeof(in_buffer), 0);
if (retcode < 0)
{
printf("*** ERROR - recv() failed \n");
exit(-1);
}
}


// Close the client socket
retcode = close(client_s);
if (retcode < 0)
{
printf("*** ERROR - close() failed \n");
exit(-1);
}

return(0);
}


Server Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#define PORT_NUM 42000 //port number for the server


int main()
{
int welcome_s; // Welcome socket descriptor
struct sockaddr_in server_addr; // Server Internet address
int connect_s; // Connection socket descriptor
struct sockaddr_in client_addr; // Client Internet address
struct in_addr client_ip_addr; // Client IP address
int addr_len; // Internet address length
char out_buffer[4096]; // Output buffer for data
char in_buffer[4096]; // Input buffer for data
int retcode; // Return code


// Create a welcome socket
welcome_s = socket(AF_INET, SOCK_STREAM, 0);
if (welcome_s < 0)
{
printf("*** ERROR - socket() failed \n");
exit(-1);
}


// Fill-in server (my) address information and bind the welcome socket
server_addr.sin_family = AF_INET; // Address family to use
server_addr.sin_port = htons(PORT_NUM); // Port number to use
server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any IP
address
retcode = bind(welcome_s, (struct sockaddr *)&server_addr,
sizeof(server_addr));
if (retcode < 0)
{
printf("*** ERROR - bind() failed \n");
exit(-1);
}


// Listen on welcome socket for a connection
listen(welcome_s, 1);


// Accept a connection. The accept() will block and then return with
// connect_s assigned and client_addr filled-in.
printf("Host IP1 connecting to IP2 \n");
addr_len = sizeof(client_addr);
connect_s = accept(welcome_s, (struct sockaddr *)&client_addr, &addr_len);
if (connect_s < 0)
{
printf("*** ERROR - accept() failed \n");
exit(-1);
}

// Copy the client IP address into IP address structure
memcpy(&client_ip_addr, &client_addr.sin_addr.s_addr, 4);

// Print message that accept completed
printf("Connected to client (IP address of client = %s port = %d) \n",
inet_ntoa(client_ip_addr), ntohs(client_addr.sin_port));

//User input prompt
printf("Please enter message to be sent, type \"exit\" to close\n");
scanf("%s",out_buffer);


while(strcmp(out_buffer,"exit") != 0)
{
// Send to client
retcode = send(connect_s, out_buffer, (strlen(out_buffer) + 1), 0);
if (retcode < 0)
{
printf("*** ERROR - send() failed \n");
exit(-1);
}

// Receive from client
retcode = recv(connect_s, in_buffer, sizeof(in_buffer), 0);
if (retcode < 0)
{
printf("*** ERROR - recv() failed \n");
exit(-1);
}
//Print messege recieved
printf("Received from client: %s \n", in_buffer);

//Get user input
scanf("%s",out_buffer);
}



// Send to client
retcode = send(connect_s, out_buffer, (strlen(out_buffer) + 1), 0);
if (retcode < 0)
{
printf("*** ERROR - send() failed \n");
exit(-1);
}


// Close the welcome and connect sockets
retcode = close(welcome_s);
if (retcode < 0)
{
printf("*** ERROR - close() failed \n");
exit(-1);
}
retcode = close(connect_s);
if (retcode < 0)
{
printf("*** ERROR - close() failed \n");
exit(-1);
}

// Return zero and terminate
return(0);
}

S-ar putea să vă placă și