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
[cmd=]netstat -a[/cmd]
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);


}
 
Code:
 hints.ai_family = AF_INET; //IPV4 
 hints.ai_socktype = SOCK_STREAM; //TCP stream sockets
[color="Red"]-hints.ai_flags = AI_PASSIVE; //fills in local IP address automatically[/color]
[color="YellowGreen"]+hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE;[/color]


 getaddrinfo(NULL, "3490", &hints, &result );
see getaddrinfo(3) for more info about AI_NUMERICSERV flag.
 
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?
 
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.
 
Back
Top