The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
recv syscall for socket programming heljy High Level Programming 2 06-16-2008 07:24 AM
Network Programming in Unix powermind High Level Programming 2 09-02-2006 07:46 AM
Network Programming in C vibhory2j High Level Programming 1 04-15-2004 12:57 PM
TCP Programming problems with 'recv' KrazyGuyPaul IP Networking 1 09-12-2002 05:17 PM
Network programming darkspace High Level Programming 2 05-21-2002 07:20 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 05-11-2007
Octal Octal is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 67
C Network Programming - recv() help

So I'm making a program that gets the IP Address of an inputed website, then sends the IP Address to an inputed host. This program has no real meaning, but just a learning experiment. So I sucessfully made the program, then I wanted to try out recv(), and it produces some weird output; here is the source:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* exit() */
#include <errno.h> /* herror() */
#include <netdb.h> /* gethostbyname() */
#include <sys/types.h> /* bind() accept() */
#include <sys/socket.h> /* bind() accept() */

#define PORT 3490

main(int argc, char *argv[]) {
	int sockfd, n_sockfd, sin_size, len;
	char *host_addr, *recv_msg;
	struct hostent *host;
	struct sockaddr_in my_addr;
	struct sockaddr_in their_addr;

	if (argc != 3) {
		fprintf(stderr, "usage: %s [hostname] [ip address]\n",
		        argv[0]);
		exit(1);
	}
	if ((host = gethostbyname(argv[1])) == NULL) {
		herror("gethostbyname");
		exit(1);
	}
	if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
		perror("socket");
		exit(1);
	}

	my_addr.sin_family = AF_INET;
	my_addr.sin_port = htons(PORT);
	my_addr.sin_addr.s_addr = inet_addr(argv[2]);
	memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);

	if ((bind(sockfd, (struct sockaddr *)&my_addr,
	     sizeof(struct sockaddr))) == -1) {
		perror("bind");
		exit(1);
	}
	if ((listen(sockfd, 10)) == -1) {
		perror("socket");
		exit(1);
	}
	sin_size = sizeof(struct sockaddr_in);
	if ((n_sockfd = accept(sockfd, (struct sockaddr *)&their_addr,
	    &sin_size)) == -1) {
		perror("accept");
		exit(1);
	}
	close(sockfd);

	host_addr = inet_ntoa(*((struct in_addr *)host->h_addr));

	len = strlen(host_addr);
	send(n_sockfd, host_addr, len, 0);
	len = strlen("\n");
	send(n_sockfd, "\n", len, 0);

	recv(n_sockfd, recv_msg, 10, 0);
	printf("%s\n", recv_msg);

	close(n_sockfd);
}
And here is my command line stuff:

The program in action:
Code:
~/c/network/testing $ ./hostinfo google.com 127.0.0.1
hello
� +������F��V��f��v����
                       ~/c/network/testing $
And the same computer interacting with it:
Code:
~ $ telnet 127.0.0.1 3490
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
64.233.167.99
hello
Connection closed by foreign host.
~ $
How what is with the � +������F��V��f��v����?

Thanks for reading,
Octal
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0