Results 1 to 1 of 1
hi i have a problem.........
i want to send a structure b/w two systems using sockets.............
but systems are not connected by lan and no server......
instead i have wlan ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 10-13-2008 #1Just Joined!
- Join Date
- Oct 2008
- Posts
- 3
problem in transfering data between two sysyems having wlan using socket
hi i have a problem.........
i want to send a structure b/w two systems using sockets.............
but systems are not connected by lan and no server......
instead i have wlan connection they are able to ping each other and also i m able to use scp..........
but for socket (stream tcp/ip) it is not working its showing
message no route to host.........
i m sending my code............
where 192.168.1.120 is my ip address.
/*This is a simple sequential server program which receives message at port 1034 ans sends the message to desired client*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define MYPORT 1034
main()
{
int sd,pid,nsd,dat,yes=1;
char message[40];
struct sockaddr_in server,client;
socklen_t length;
//Creating un named socket
if((sd=socket(PF_INET,SOCK_STREAM,0))==-1) {
perror("socket");
exit(1);
}
server.sin_port=htons(MYPORT);
server.sin_family=PF_INET;
server.sin_addr.s_addr=inet_addr("192.168.1.120");
//Nameing the scoket using bind(2) which also binds the scoket to LOCAL ADDRESS
if(bind(sd,(struct sockaddr *)&server,sizeof(server))==-1) {
perror("bind:");
exit(1);
}
//Listen for connections on scoket
if(listen(sd,5)==-1) { //backlog is 5 pending connections can grow
perror("listen");
exit(1);
}
printf("Waiting for connection.............\n");
//Accept connection on socket, then return new socket descriptor
if((nsd=accept(sd,(struct sockaddr *)&client,&length))==-1) {
perror("accept");
exit(1);
}
//Manipualte inter net address, converter.
//INTERNET to network - byte - order
printf("Got connection from client:%s\n",inet_ntoa(client.sin_addr));
//Recive message from the socket
if((dat=recv(nsd,message,40,0))==-1) {
perror("recv");
exit(1);
}
message[dat]='\0';
printf("Data received is : %s\n",message);
printf("Enter the data you want to send to client\n");
fgets(message,40,stdin); //Reading from stdin
//Write message to socket
send(nsd,message,40,0);
close(sd);
close(nsd);
}//End of the program
/* This is a simple tcpclient which connects to the server and sends a message*/
#include<sys/socket.h>
#include<stdlib.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#define PORT 1034
struct sockaddr_in server;
main()
{
int n,sd,length;
char msg[40];
if((sd=socket(PF_INET,SOCK_STREAM,0))==-1) {
perror("socket");
exit(1);
}
server.sin_family=PF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=inet_addr("192.168.1.120");
//initiate the connection on socket
if(connect(sd,(struct sockaddr *)&server,sizeof(server))==-1) {
perror("In client connect \n");
exit(1);
}
printf("Enter the message you want to send to server\n");
fgets(msg,40,stdin);//Read message from stdin
send(sd,msg,40,0); //send message to server
printf("Waiting for message from server..............\n");
//bolcked recive
n=recv(sd,msg,40,0);//Receving message from server
msg[n]='\0';
printf("Message received from server is:%s\n",msg);
close(sd);
}


Reply With Quote
