Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 24th, 2006, 5:50 PM   #1
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
more on sockets

Alright two questions:
1) Right now i run my client application, i enter the hostname and i use the getbyhostname() function to get its IP. then connect that host on whatever port. I want to prompt the user for the ip to connect to instead of hostname. so the user enters a string xxx.xxx.xxx.xxx and i want i need to set the struct sockaddr_in 's IP address to the IP address entered then connect to it. so how do i do that? how do i connect to a server just by knowing the IP address of that server and the port i want to connect to.
2) i use the sendto() function to send a certain char* from client to server. my server recieves it using:
int clen = sizeof (sockaddr_in);
		int size = 	recvfrom(sd,temp,strlen(temp),0,(struct sockaddr *) &client,&clen);	//get recieved input and put in temp
recvfrom returns the size of the buffer recieved. so if i send the string "hello" i recieve "hello" on the server with a length of 5. now how can i send the string "hello server" with a space between them? i did some investigation on this. at first i thought that i actually recive "hello server" but the size returned by the recvfrom() function would be 5 instead of 12. but i tried printing buffer[7] which should be "s" and that did not work. so i came to the conclusion that the send function apparently cuts the string off at the space, maybe it considers it a null..

thanks for the help guys. sorry i blabbed alot, just trying to clarify.
hbe02 is offline   Reply With Quote
Old May 24th, 2006, 6:05 PM   #2
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
ok i found an answer to my first question... use this line of code:
	sendhost.sin_addr.S_un.S_addr  =	inet_addr(IP);
where IP is a char* xxx.xxx.xxx.xxx
and sendhost is of type sockaddr_in
hbe02 is offline   Reply With Quote
Old May 24th, 2006, 7:49 PM   #3
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
strlen returns the number of bytes (char's) already in the arrary. Perhaps you want sizeof(temp); instead.
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:41 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC