PDA

View Full Version : C++ Socket programming




tr!pf!3
Aug 2, 2012, 02:23 AM
Some issues with the os i think. Firewall shouldnt be an issue, the program that I have created is allowed to accept connections and send across them. When I setup a second socket, and then do a netstat -an call its blocking. Furthermore, I do a sudo lsof | grep "program" ... it shows this


program 9917 K7 10u IPv4 0x0da942b8 0t0 TCP *:* (CLOSED)


It makes it impossible to communicate with servers after this happens. Both sockets are setup as non blocking as well. Does anybody have an idea why this would happen?



jpyc7
Aug 2, 2012, 08:11 AM
You said your program does two things: accept sockets and send across them. To do both in the same program means you will need a multi-threaded program. I think you want to separate your program into two different programs, a "server" (to accept connections) and a "client" (to connect).

It also seems a bit strange that your lsof output shows *:*, suggesting that you don't have an IP address or port number.

Without some code, it's pretty hard for anyone to diagnose your problem.

cqexbesd
Aug 2, 2012, 03:55 PM
It also seems a bit strange that your lsof output shows *:*, suggesting that you don't have an IP address or port number.


That's exactly the issue here. I suspect the OP has called socket but hasn't bound or connected it to anything so atm the socket doesn't have an address.

OP: you might need to read a tutorial. I'm sure there are many on the web or find a copy of Steven's UNIX Network Programming.


Without some code, it's pretty hard for anyone to diagnose your problem.

That I can agree with.

Andrew

softwareguy256
Aug 2, 2012, 09:30 PM
basics from someone who's been in the business for a bit:

run it on localhost

tr!pf!3
Aug 3, 2012, 01:39 AM
Ok ok ok, so I dont seem a bit shady here, here is some code. I know that how i explained it probably was not exact and caused some confusion.


bool CREATE(SOCKET_DATA *PIPE) {
PIPE->m_sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( !IS_VALID( PIPE ) ) {
return false;
}

linger LING;
unsigned int LINGER_SIZE = sizeof(LING);
LING.l_onoff=1;
LING.l_linger=10;

int OPTION = 1;

if ( setsockopt( PIPE->m_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&OPTION, sizeof( OPTION ) ) == -1 ) {
return false;
}

if ( setsockopt( PIPE->m_sock, SOL_SOCKET, SO_LINGER, (void*)(&LING), LINGER_SIZE ) == -1 ) {
return false;
}

return true;
}

Now first I setup a server socket to listen, and after that I have a bind, listen and accept socket which all works perfectly, plus a non blocking function, again working perfectly. The other function sets up a client socket, connecting to a seperate server, web server or whatever. The purpose of the two sockets is to transfer data between. At this point you understand that this program is a kind of proxy, which it is.

So after I try to setup a client socket, after calling the create_socket function, right this line,

PIPE->m_sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

run those two commands through terminal, "netstat" and "lsof" and tada we have the CLOSED issue. Before that, it doesnt exist, but aha we have established a connection and a listen.

program 11413 K7 4u IPv4 0x0da8ead8 0t0 TCP *:21172 (LISTEN)
program 11413 K7 5u IPv4 0x0da93ea8 0t0 TCP 17.172.232.12:21172->17.172.232.12:52561 (ESTABLISHED)
program 11413 K7 6u IPv4 0x0da88688 0t0 TCP 172.16.1.253:52562->cplusplus.com:http (CLOSE_WAIT)
program 11413 k7 8u IPv4 0x0fdd0f68 0t0 TCP *:* (CLOSED) **This is the problem, right here**


But it shows "above" that a connection was established. My functions work correctly, but setting up the second socket literally makes the program just go like omg omg omg omg. lol

unless there's a problem with my pointers, but I dont think so.

cqexbesd
Aug 3, 2012, 03:03 PM
So after I try to setup a client socket, after calling the create_socket function, right this line,

PIPE->m_sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

run those two commands through terminal, "netstat" and "lsof" and tada we have the CLOSED issue. Before that, it doesnt exist, but aha we have established a connection and a listen.

program 11413 K7 4u IPv4 0x0da8ead8 0t0 TCP *:21172 (LISTEN)
program 11413 K7 5u IPv4 0x0da93ea8 0t0 TCP 17.172.232.12:21172->17.172.232.12:52561 (ESTABLISHED)
program 11413 K7 6u IPv4 0x0da88688 0t0 TCP 172.16.1.253:52562->cplusplus.com:http (CLOSE_WAIT)
program 11413 k7 8u IPv4 0x0fdd0f68 0t0 TCP *:* (CLOSED) **This is the problem, right here**

But it shows "above" that a connection was established. My functions work correctly, but setting up the second socket literally makes the program just go like omg omg omg omg. lol


It doesn't seem to be a problem to me. You have asked for a TCPv4 socket and that's what you've got. It shows up in lsof output as having no associated address because it doesn't have any associated address because you haven't associated any address with it. Either bind the socket, or connect it. Either operation will cause address association.

HTH,

Andrew

tr!pf!3
Aug 3, 2012, 03:44 PM
Ok, yes you are correct, I setup a smaller version of this program and did the exact same thing. As long as it follows this form... you should be good, otherwise the program will block all ports.


int main(int argc, char **argv) {

int SOCKET_1, SOCKET_2;
struct sockaddr_in m_addr;

SOCKET_1 = socket(AF_INET, SOCK_STREAM, 0);

if ( SOCKET_1 == -1 ) {
printf("Error creating socket #1\n");
}

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

m_addr.sin_family = AF_INET;
m_addr.sin_addr.s_addr = INADDR_ANY;
m_addr.sin_port = htons ( 21272 );

if ( bind( SOCKET_1, (struct sockaddr*)&m_addr, sizeof( m_addr ) ) == -1 ) {
printf("Error binding socket 1\n");
}

if ( listen( SOCKET_1, 5 ) < 0 ) {
printf("Error listening on socket #1\n");
}

printf("SLEEPING 30 SECONDS...\n");

sleep(30);

SOCKET_2 = socket(AF_INET, SOCK_STREAM, 0);

if ( SOCKET_2 == -1 ) {
printf("Error creating socket #2\n");
}

**It's right around this part where I had something totally different, something similar to what was above. I went about it differently like this. **

hostent* record = gethostbyname("www.cplusplus.com");

if (record == NULL) {
herror("gethostbyname failed");
return EXIT_FAILURE;
}

in_addr* addressptr = (in_addr *) record->h_addr;

sockaddr_in server_info;
server_info.sin_len = sizeof(server_info);
server_info.sin_family = AF_INET;
server_info.sin_addr = *addressptr;
server_info.sin_port = htons(80);

if ( connect( SOCKET_2, (sockaddr*) &server_info, sizeof(server_info)) < 0) {
perror("Connection failed.");
return EXIT_FAILURE;
}

printf("SLEEPING 1 MINUTE 40 SECONDS...\n");

sleep(100);

return EXIT_SUCCESS;
}

Once you type in the command lsof and search up the program, there shouldnt be a marked CLOSED line.

mfram
Aug 3, 2012, 06:41 PM
I don't see any accept() call in there. accept() is the call that blocks, waits for a new connection to come in to the server, and creates the new socket which represents the new TCP connection. Without accept(), you aren't getting new connections from your listening socket.

tr!pf!3
Aug 4, 2012, 03:04 AM
I know that, it was only a test. You can put it in though lol.