c7bb network programming, successful telnet connection, but can not communicate - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, Shell, Perl, Sed & Awk

Reply
 
Thread Tools Display Modes
  #1  
Old April 14th, 2012, 03:28
Vitamin Vitamin is offline
Junior Member
 
Join Date: Jan 2012
Posts: 37
Thanks: 7
Thanked 0 Times in 0 Posts
Default network programming, successful telnet connection, but can not communicate

telnet session will connect, but I cannot communicate during session. I don't see the customized welcome message for each new accepted connection that is in the program. I type in the telnet window, but the program doesn't receive the message.

If I do
netstat -a
I see the Recv-Q with bytes of pending data, but Send-Q is empty. I executed different programming projects, from different websites, but same results. So I assume that my firewall settings are to blame.

[ see http://forums.freebsd.org/showthread.php?t=31340 -- Mod. ]

Please, any suggestions?

Code:
 int status, socketFileDesc, newFileDesc, bytes_sent, len;
 struct addrinfo hints;
 struct addrinfo *result;
 struct sockaddr_storage their_addr;
 socklen_t addr_size;
 char buff[4096];

 memset(&hints, 0, sizeof(hints)); 

 
 hints.ai_family = AF_INET; //IPV4 
 hints.ai_socktype = SOCK_STREAM; //TCP stream sockets
 hints.ai_flags = AI_PASSIVE; //fills in local IP address automatically


 getaddrinfo(NULL, "3490", &hints, &result );

 socketFileDesc = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
 bind(socketFileDesc,result->ai_addr, result->ai_addrlen);
 listen(socketFileDesc, BACKLOG);

 strcpy(buff, "Hello Process \n");

 while(1){
 addr_size = sizeof(their_addr);
 newFileDesc = accept(socketFileDesc, (struct sockaddr *) &their_addr, &addr_size);

 len = strlen(buff);
 send(newFileDesc, buff,len , 0);


}

Last edited by DutchDaemon; April 14th, 2012 at 04:43.
Reply With Quote
  #2  
Old April 14th, 2012, 11:45
PseudoCylon PseudoCylon is offline
Member
 
Join Date: Oct 2009
Location: Alberta, Canada
Posts: 142
Thanks: 1
Thanked 17 Times in 12 Posts
Default

Code:
 hints.ai_family = AF_INET; //IPV4 
 hints.ai_socktype = SOCK_STREAM; //TCP stream sockets
-hints.ai_flags = AI_PASSIVE; //fills in local IP address automatically
+hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE;


 getaddrinfo(NULL, "3490", &hints, &result );
see getaddrinfo(3) for more info about AI_NUMERICSERV flag.
__________________
Don't ask what Beastie can do for you; ask what you can do for Beastie!
Reply With Quote
  #3  
Old April 14th, 2012, 20:50
Vitamin Vitamin is offline
Junior Member
 
Join Date: Jan 2012
Posts: 37
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Thanks for the suggestion PseudoCylon but same result.

I decided to use the "older" way of setting up for socket programming. I was able to get it to work temporarily, but I'm not sure how since I deleted and redone all the steps in my first thread that is linked in the first post.

But after a while of working (displaying hello to all new connections), it would just connect and not display anything. I would have to restart the entire computer to get it to work temporarily again, then it would stop again.

Here is the code I got to temporarily work from a website.

Code:
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#define BACKLOG 10 //pending connections queued


int main()
{
  int servsock, backlog, err,cliLen, clisock, numBytes;
  struct sockaddr_in servaddr, cliaddr;
   servsock = socket( AF_INET, SOCK_STREAM, 0);
  
   memset( &servaddr, 0, sizeof(servaddr) );
  
   servaddr.sin_family = AF_INET;
   servaddr.sin_port = htons(3490);
   servaddr.sin_addr.s_addr = inet_addr("192.168.0.111");
 
   char buff[4096];

   err = bind( servsock, (struct sockaddr *)&servaddr,sizeof(servaddr));
  listen(servsock,BACKLOG);


  strcpy(buff, "Hello Process");

  while(1){
 cliLen = sizeof(cliaddr);
 clisock = accept( servsock, (struct sockaddr *)&cliaddr, &cliLen );

 send(clisock,buff, strlen(buff), MSG_DONTWAIT);
 
 //numBytes = recv(clisock, buff,sizeof(buff),MSG_DONTWAIT);
   // recv(clisock, buff,sizeof(buff),  MSG_PEEK);


 //printf(buff);
 close(clisock);
  }
}

Are there any terminal commands to free up any references to a socket that has been closed? Or close a socket that is listening, even though the program that initialized it has already been closed?

Last edited by DutchDaemon; April 15th, 2012 at 02:33.
Reply With Quote
  #4  
Old April 14th, 2012, 21:58
Vitamin Vitamin is offline
Junior Member
 
Join Date: Jan 2012
Posts: 37
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Just researched how to kill processes. I'm using the "old version" of socket programming that I posted above. I'll kill the suspecting processes that may still be referencing ports, even though my program closed.

So the following steps I will use when there is too much reference to the same port and/or too many anonymous references to ports; which may have occurred as I continually ran/stop the program.


Code:
netstat -a
To check if there are too much anonymous references to *[port number] and to check if the port specified in my program is in LISTEN mode, though the program has already closed.

Code:
lsof -i -n
Display which process has a anonymous reference to the *[port number] or reference the specific port that is used in my program. I noticed my program had 9 references to random ports in addition to the port I specified in the program, even though my program was closed.

Code:
kill -9 PID
Use this to kill the processes.

These steps are a temporary solution for, until I find a permanent solution for this issue. It keeps me from continually having to restart my program.

Thanks for all your help guys.

EDIT: make sure you have root credentials while in the terminal that you are running the above code, this ensures you get more information when you run the lsof command.

Last edited by DutchDaemon; April 15th, 2012 at 02:35. Reason: Mind your writing style: http://forums.freebsd.org/showthread.php?t=18043
Reply With Quote
Reply

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
PF firewall setting for network programming Vitamin Firewalls 6 April 14th, 2012 03:16
unstable network connection frankcheong Networking 8 January 4th, 2011 03:25
Odd network connection behavior. Avanesov Networking 5 October 4th, 2010 16:25
Getting a network connection in VMWare rl2hb Installing & Upgrading 4 February 27th, 2009 15:29
Check connection with timeout, script telnet web server in script bsddaemon Web & Network Services 5 December 7th, 2008 14:03


All times are GMT +1. The time now is 16:52.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0