Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > Your Distro > Redhat / Fedora Linux Help > problem in transfering data between two sysyems having wlan using socket

Forgot Password?
 Redhat / Fedora Linux Help   Help and discussion related to Redhat and Fedora Linux.

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 10-13-2008   #1 (permalink)
Just 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);

}
amit12sept is offline  


Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 10:23 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2