Visit The New, Modern Unix Linux Community

C Network Programming - recv() help

Thread Tools Search this Thread
Top Forums Programming C Network Programming - recv() help
# 1  
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:
#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",
	if ((host = gethostbyname(argv[1])) == NULL) {
	if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -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) {
	if ((listen(sockfd, 10)) == -1) {
	sin_size = sizeof(struct sockaddr_in);
	if ((n_sockfd = accept(sockfd, (struct sockaddr *)&their_addr,
	    &sin_size)) == -1) {

	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);


And here is my command line stuff:

The program in action:
~/c/network/testing $ ./hostinfo
� +������F��V��f��v����
                       ~/c/network/testing $

And the same computer interacting with it:
~ $ telnet 3490
Connected to
Escape character is '^]'.
Connection closed by foreign host.
~ $

How what is with the � +������F��V��f��v����?

Thanks for reading,
# 2  
You are checking the return code from the socket call. Do the same with recv() and send(). If recv fails, the buffer is untouched and you just print whatever garbage was in it.
# 3  
I actually figured it out:
bytes = recv(n_sockfd, recv_msg, 10, 0);
recv_msg[bytes] = '\0';
printf("%s\n", recv_msg);

And bytes was obviously an int that I had to declare.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #928
Difficulty: Medium
64-bit versions of the Unix time stamp will cease to work at 15:30:08 UTC on Sunday, 14 December 2922770265960.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How does unix system administration, unix programming, unix network programming differ?

How does unix system administration, unix programming, unix network programming differ? Please help. (0 Replies)
Discussion started by: thulasidharan2k
0 Replies

2. Programming

Unix Network Programming

I have written a client-server program which does some data from a file in server to the client. In this I don't want the client to wait indefinitely if server is not running. For this I am using SELECT system call, in this system call we can specify timings as an argument, which tells the client... (2 Replies)
Discussion started by: naresh046
2 Replies

3. Programming

Unix network programming

Hi! I am working on fedora.. trying to execute BSD4.4 client-server program which includes "unp.h" header file... While executing make command, I got error like, " expected " , " , " ; ",or ")" in connect_nonb file...ERROR 1 " I tried to change mode of makefile but I can't get... (4 Replies)
Discussion started by: nisha_vaghela
4 Replies

4. Programming

recv syscall for socket programming

I have a question regarding the recv syscall. Suppose I have a client/server and the following exchange of message took place: Client --> Server using multiple send syscalls one after another immediately: send "Packet1" send "Packet2" send "Packet3" Server receives in the... (2 Replies)
Discussion started by: heljy
2 Replies

5. Programming

Network Programming in Unix

Good day everyone, Please help if you are interested in. I need to do a chat client-server program. Does anyone know where I can get references or sample programs? Thank you very much for your time Eric (2 Replies)
Discussion started by: powermind
2 Replies

6. Programming

Network Programming in C

hello, i am learning networking programming in C from Unix Networking Programing by W. Richards Stevens. i want to compile the source given in this book on windows and linux platform. if somebody know this on windows, pls let me know. and regarding Linux, every time i compile a program on gcc,... (1 Reply)
Discussion started by: vibhory2j
1 Replies

7. Programming

Want To Learn Network Programming

I want to learn Network Programming with C,but I don't know how to start. Thank you. (2 Replies)
Discussion started by: hubin330
2 Replies

8. UNIX for Dummies Questions & Answers

Unix Network Programming

I am going to purchase Unix Network Programming by Stevens. The only question I have is which edition to buy. The older (early 90's) edition, or the newer (late 90's) edition. I know conventional thinking would point to the latest ( and greatest?), but I wanted to get some feedback from the forum.... (3 Replies)
Discussion started by: dangral
3 Replies

9. IP Networking

TCP Programming problems with 'recv'

Hey, I am learning to program a TCP server and managed to get it up and running (I am using Windows 98SE). I can use the send function to send information to the client and I can use the recv function to ask the user to pass information through, but when I do so it only allows the client to... (1 Reply)
Discussion started by: KrazyGuyPaul
1 Replies

10. Programming

Network programming

Hi, I`m trying to do some multicast programming, and i`m looking for a C-function to convert an Interfacename to an IP-address or/and a C-function to convert an Interfaceindex to an IP-address. I need it for the mcast_set_if(int sockfd, const char *ifname (!), u_int ifindex(!)) function by... (2 Replies)
Discussion started by: darkspace
2 Replies

Featured Tech Videos