Programming/Network Programming
get client IPadrress in server side
박세범
2011. 10. 10. 01:54
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
struct sockaddr_in local;
struct sockaddr_in client;
int *csize;
int s; int s1; int rc; char buf[1];
local.sin_family = AF_INET;
local.sin_port = htons(5300);
local.sin_addr.s_addr = htonl(INADDR_ANY);
char getTime[255];
//added by Justin
printf("A server has been started, the port number is reserved as 5300\n\n");
s = socket(PF_INET, SOCK_STREAM, 0);
if(s<0){
perror("socket call failed");
exit(1);
}
rc = bind(s, (struct sockaddr *)&local, sizeof(local));
if(rc<0){
perror("bind call failure");
exit(1);
}
printf("A server is waintg for a client request\n\n");
rc = listen(s,5);
if(rc){
perror("listen call failed");
exit(1);
}
csize = sizeof(client); // this is used to get a ip address using accept()
s1= accept(s,(struct sockaddr*)&client ,&csize); // we need to know the ip address of a client
// to get client address. then we have to send this client
// address to the client.
printf("Conntection established!\n\n");
if(s1<0){
perror("accept call failed");
exit(1);
}
rc = recv(s1,getTime ,sizeof(getTime), 0); // rc return the size of the data
if(rc<=0){
perror("recv call failed");
exit(1);
}
printf("\nCurrent time from client : %s \n", getTime); //print current Time
printf("\n\nA server will send a client's IP address \n");
rc = send(s1, &client , sizeof(client), 0); // send a structure of sockaddr_in to the client
if(rc <= 0) // if it failes
{
perror("send call failed");
exit(0);
}
}
accept should have the sockaddr struct as long as we want to know about client's address information
In this example, client.sin_addr has a IP address of a client and client.sin_port has a port #
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
struct sockaddr_in local;
struct sockaddr_in client;
int *csize;
int s; int s1; int rc; char buf[1];
local.sin_family = AF_INET;
local.sin_port = htons(5300);
local.sin_addr.s_addr = htonl(INADDR_ANY);
char getTime[255];
//added by Justin
printf("A server has been started, the port number is reserved as 5300\n\n");
s = socket(PF_INET, SOCK_STREAM, 0);
if(s<0){
perror("socket call failed");
exit(1);
}
rc = bind(s, (struct sockaddr *)&local, sizeof(local));
if(rc<0){
perror("bind call failure");
exit(1);
}
printf("A server is waintg for a client request\n\n");
rc = listen(s,5);
if(rc){
perror("listen call failed");
exit(1);
}
csize = sizeof(client); // this is used to get a ip address using accept()
s1= accept(s,(struct sockaddr*)&client ,&csize); // we need to know the ip address of a client
// to get client address. then we have to send this client
// address to the client.
printf("Conntection established!\n\n");
if(s1<0){
perror("accept call failed");
exit(1);
}
rc = recv(s1,getTime ,sizeof(getTime), 0); // rc return the size of the data
if(rc<=0){
perror("recv call failed");
exit(1);
}
printf("\nCurrent time from client : %s \n", getTime); //print current Time
printf("\n\nA server will send a client's IP address \n");
rc = send(s1, &client , sizeof(client), 0); // send a structure of sockaddr_in to the client
if(rc <= 0) // if it failes
{
perror("send call failed");
exit(0);
}
}
accept should have the sockaddr struct as long as we want to know about client's address information
In this example, client.sin_addr has a IP address of a client and client.sin_port has a port #