How To Print The Client Address At The Server | Source Code In C Network Programming

To write a program in c to print the client address at the server.
Algorithm for C Programming Source code
Server side Algorithm
STEP 1: Start the program.
STEP 2: Declare the variables and structure for the socket.
STEP 3: The socket is binded at the specified port.
STEP 4: Using the object the port and address are declared.
STEP 5: The listen and accept functions are executed.How To Printing The Client Address At The Server Source Code In C Network Programming
STEP 6: If the binding is successful then the client address is printed .
STEP 7: Execute the client program.
STEP 8: close the socket function.
Client side algorithm
STEP 1: Start the program.
STEP 2: Declare the variables and structure.
STEP 3: Socket is created and connect function is executed.
STEP 4: If the connection is successful then server sends the message.
STEP 5: Get the server IP address from the client.
STEP 6: Print the message that is read from the client.
STEP 6: Stop the program
Server programming source code in c Print the client address at the server
#include<netinet/in.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
main()
{
struct sockaddr_in sa;
struct sockadd_in cli;
int socketfd, conntfd;
int len;
char str[100];
time_t tick;
socketfd=socket(AF_INET,SOCK_STREAM,0);
if(socketfd<0)
{
print(“\n ERROR IN SOCKET\n”);
exit(0);
{
else
printf(“\n SOCKET OPENED\n”);
bzero(&sa, sizeof(sa));
sa.sin_family=AF_INET;
sa.sin_port=htons(5600);
if(bind(socketfd,(struct sockaddr*)&sa, sizeof(sa))<0)
{
printf(“\nERROR FINDING FAILED\n”);
exit(0);
}
else
printf(“\nBINDED SUCCESSFYLLY\n);
listen(socketfd,50);
for(;;)
{
len=sizeoof(cli);
conntfd=accept(socketfd,(struct sockaddr*)&cli,&len);
printf(“\nACCEPTED\n);
printf(“\nREQUEST FROM%sPORT%d\n”,inet-ntop(AF_INET,&cli.
sin_addr,str,sizeof(str)),htons(cli.sin_port));
tick=time(NULL);
snprintf(str,sizeof(str),”%s”,ctime(&tick));
write (conntfd,str, sizeof(str));
}
}
Client Program Side Source code programming How to Print the client address at the server
#include<netinet/in.h>
#include<sys/socket.h>
main()
{
struct sockaddr_in sa;
struct sockaddr_in cli;
int n,socketfd;
int len;
char buff[100]
socketfd=socket(AF_INET,SOCK_STREAM,0);
if(socketfd<0)
{
printf(“\ERROR IN SOCKET\n”);
exit(0);
}
else
printf(“\n SOCKET IS OPENED\N”);
bzero(&sa,sizeof(sa));
sa.sin_family=AF_INET;
sa.sin_port=htons(5600);
if(connect(socketfd,(struct sockaddr*)&sa,sizeof(sa))<0)
{
print(“ERROR IN CONNECTION\n”);
exit(0);
}
else
printf(“\nCONNECTED SUCCESSFULLY\n”);
if(n=read(socketfd,buff,sizeof(buff))<0)
{
printf(“\nERROR IN READING\n”);
exit(0);
}
else{
print(“\nMESSAGE READ%s”,buff);
buff[n]=’\0’;
printf(“%s”,buff);
}
}
Output CS1305 Network Lab
CLIENT side status when socket open
Socket is opened
Connected successfully
Message read Wed Feb 711:52:58 2007
SERVER side status after connect with client and print client address
Socket is opened
Binded successfully
Accepted
Request from 127.0.0.1 port 33584
RESULT Thus the program to print the client address at the server is executed
and verified. CS1305 Network Lab - Free C programming Source code.

Post a Comment

1 Comments

Quintin S said…
This is a great post, thanks for sharing it.