This is a discussion on TCP connection refused within the Linux Security forums, part of the System Security and Security Related category; Hello I wonder if anybody can help me.. I have got two linux machines, both running fedora core 2, and ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello
I wonder if anybody can help me.. I have got two linux machines, both running fedora core 2, and a windows machine (running XP) - I have built a TCP client and server for linux (and one for windows), these all work fine when connecting to localhost - however they can't seem to connect to each other - the connet() call always errors with "connection refused" - *both* when trying to do it from windows to linux (linux running the server), *and* linux to linux (linux running the server and the client). Everything else network related works - the windows machine can see the linux files via the samba server and vice versa, and the windows web browser can see the linux box's web server's apache home page, and the linux machines can both see each other's files, and browse to each other's web servers. It's just my own TCP programs that always seem to refuse to connect. It's always "Connection refused". I'm stumped as to what I can do, because the programs work fine when connecting to localhost / 127.0.0.1 - just not to each other. Any ideas? |
|
|||
|
Bonj wrote:
> Hello > I wonder if anybody can help me.. > I have got two linux machines, both running fedora core 2, and a windows > machine (running XP) - I have built a TCP client and server for linux (and > one for windows), these all work fine when connecting to localhost - > however they can't seem to connect to each other - the connet() call always > errors with "connection refused" - *both* when trying to do it from windows > to linux (linux running the server), *and* linux to linux (linux running the > server and the client). > > Everything else network related works - the windows machine can see the > linux files via the samba server and vice versa, and the windows web browser > can see the linux box's web server's apache home page, and the linux > machines can both see each other's files, and browse to each other's web > servers. > It's just my own TCP programs that always seem to refuse to connect. It's > always "Connection refused". I'm stumped as to what I can do, because the > programs work fine when connecting to localhost / 127.0.0.1 - just not to > each other. > > Any ideas? This sounds like what would happen if the port number is not specified in network byte order at one end. -- Scott McPhillips [VC++ MVP] |
|
|||
|
On Sat, 26 Feb 2005 00:49:07 -0000
"Bonj" <a@b.com> wrote: > It's just my own TCP programs that always seem to refuse to connect. It's > always "Connection refused". I'm stumped as to what I can do, because the > programs work fine when connecting to localhost / 127.0.0.1 - just not to > each other. > > Any ideas? .... first - do NOT cross-post to so many groups. narrow the included newsgroups to one's that match your subject matter. second - find a good book on TCP/IP network programming. arguably, the best are the TCP/IP & "Networking" series from Addison-Wesley ... then check your code against the expertise provided by the authors. -- << http://michaeljtobler.homelinux.com/ >> "Consequences, Schmonsequences, as long as I'm rich." -- "Ali Baba Bunny" [1957, Chuck Jones] |
|
|||
|
I use
service.sin_port = htons(PORT_NO); function in C on windows, as in linux. It doesn't work even with this function. Isn't this enough? "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message news:DYKdnSN4OKf2VILfRVn-jw@comcast.com... > Bonj wrote: > >> Hello >> I wonder if anybody can help me.. >> I have got two linux machines, both running fedora core 2, and a windows >> machine (running XP) - I have built a TCP client and server for linux >> (and one for windows), these all work fine when connecting to localhost - >> however they can't seem to connect to each other - the connet() call >> always errors with "connection refused" - *both* when trying to do it >> from windows to linux (linux running the server), *and* linux to linux >> (linux running the server and the client). >> >> Everything else network related works - the windows machine can see the >> linux files via the samba server and vice versa, and the windows web >> browser can see the linux box's web server's apache home page, and the >> linux machines can both see each other's files, and browse to each >> other's web servers. >> It's just my own TCP programs that always seem to refuse to connect. It's >> always "Connection refused". I'm stumped as to what I can do, because the >> programs work fine when connecting to localhost / 127.0.0.1 - just not to >> each other. >> >> Any ideas? > > This sounds like what would happen if the port number is not specified in > network byte order at one end. > > -- > Scott McPhillips [VC++ MVP] > |
|
|||
|
"mjt" <mjtobler@removethis_mail.ru> wrote in message news:20050225190201.3bd705d2.mjtobler@removethis_m ail.ru... > On Sat, 26 Feb 2005 00:49:07 -0000 > "Bonj" <a@b.com> wrote: > >> It's just my own TCP programs that always seem to refuse to connect. It's >> always "Connection refused". I'm stumped as to what I can do, because the >> programs work fine when connecting to localhost / 127.0.0.1 - just not to >> each other. >> >> Any ideas? > > ... first - do NOT cross-post to so many groups. narrow > the included newsgroups to one's that match your subject > matter. > > second - find a good book on TCP/IP network programming. > arguably, the best are the TCP/IP & "Networking" series > from Addison-Wesley ... then check your code against > the expertise provided by the authors. > > -- > << http://michaeljtobler.homelinux.com/ >> > "Consequences, Schmonsequences, as long as I'm rich." > -- "Ali Baba Bunny" [1957, Chuck Jones] Thanks |
|
|||
|
Ah..great! it was this, thanks Scott.
I'm not sure why it wasn't working before, but I changed the listening address to htonl(IADDR_ANY) and it worked fine...this doesn't help me do the same to my C# service but never mind. Another problem perhaps you can advise me on... now I've got it working, data is being sent back and forward reliably and the number of bytes sent is always right. However I've got a problem that's a bit more of a logic issue - I'll describe what happens: the client connects to the server, and sends data first. The server recv()s this data successfully. However I would like it so that the server can receive all the data in multiple chunks (i.e. in a loop with a call to recv on each iteration)... but the problem is knowing how to stop this loop. The bit I can't get my head round is... the call to recv never returns 0 - it returns positive integer while there is still data (which fills the buffer correctly) - but then blocks indefinitely when all the data has been received... *unless* - it sends some data back on each iteration, as in an echo server. But what if I want to receive all the data before formulating the response? I could think of one solution which is to designate a zero-terminator for the data. But this is slightly messy as it requires extra code, possibly with a loop, to detect this terminator byte. I don't want to send dummy data over the network as this is causing unnecessary traffic. The code I've got for the server is: // std::string data_to_consider(""); char buf[RECVBUFLEN + 1]; do { int bytesrecvd = recv(Socket, buf, RECVBUFLEN, 0); Sleep(0); if(bytesrecvd == SOCKET_ERROR) throw(5); if(bytesrecvd > 0) { buf[min(bytesrecvd, RECVBUFLEN)] = '\0'; printf("%s", buf); //in the non-demo application this would be probably appended to a std::string or something instead... printf("Sent %d bytes back\n", send(Socket, buf, bytesrecvd, 0)); //don't want to do this every time! //instead I want to do this.. //data_to_consider += buf; } else break; //this break never occurs } while(TRUE); //here is where I want to send some data back, once and only, e.g. send(Socket, ProcessAllData(data_to_consider), data_to_consider.length, 0); printf("\n"); _tprintf(_T("\nClient has disconnected\n")); does that make sense what I'm trying to get at? Thanks! "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp> wrote in message news:DYKdnSN4OKf2VILfRVn-jw@comcast.com... > Bonj wrote: > >> Hello >> I wonder if anybody can help me.. >> I have got two linux machines, both running fedora core 2, and a windows >> machine (running XP) - I have built a TCP client and server for linux >> (and one for windows), these all work fine when connecting to localhost - >> however they can't seem to connect to each other - the connet() call >> always errors with "connection refused" - *both* when trying to do it >> from windows to linux (linux running the server), *and* linux to linux >> (linux running the server and the client). >> >> Everything else network related works - the windows machine can see the >> linux files via the samba server and vice versa, and the windows web >> browser can see the linux box's web server's apache home page, and the >> linux machines can both see each other's files, and browse to each >> other's web servers. >> It's just my own TCP programs that always seem to refuse to connect. It's >> always "Connection refused". I'm stumped as to what I can do, because the >> programs work fine when connecting to localhost / 127.0.0.1 - just not to >> each other. >> >> Any ideas? > > This sounds like what would happen if the port number is not specified in > network byte order at one end. > > -- > Scott McPhillips [VC++ MVP] > |
|
|||
|
"Bonj" <a@b.com> wrote in message news:38a5ihF5lsvv2U1@individual.net... > However I've got a problem that's a bit more of a logic issue - I'll > describe what happens: the client connects to the server, and sends data > first. > The server recv()s this data successfully. > However I would like it so that the server can receive all the data in > multiple chunks (i.e. in a loop with a call to recv on each iteration)... > but the problem is knowing how to stop this loop. The bit I can't get my > head round is... the call to recv never returns 0 - it returns positive > integer while there is still data (which fills the buffer correctly) - but > then blocks indefinitely when all the data has been received... *unless* - > it sends some data back on each iteration, as in an echo server. But what > if I want to receive all the data before formulating the response? Don't call 'recv' unless there's more data coming. Look at the data you already got to see if there's more coming. > I could think of one solution which is to designate a zero-terminator for > the data. But this is slightly messy as it requires extra code, possibly > with a loop, to detect this terminator byte. I don't want to send dummy > data over the network as this is causing unnecessary traffic. Surely it's necessary to tell when you have all the data, isn't it? How is that "unnecessary"? > The code I've got for the server is: > > // std::string data_to_consider(""); > char buf[RECVBUFLEN + 1]; > do > { > int bytesrecvd = recv(Socket, buf, RECVBUFLEN, 0); > Sleep(0); What's the Sleep for? > if(bytesrecvd == SOCKET_ERROR) throw(5); > if(bytesrecvd > 0) > { > buf[min(bytesrecvd, RECVBUFLEN)] = '\0'; > printf("%s", buf); //in the non-demo application this would be probably > appended to a std::string or something instead... > printf("Sent %d bytes back\n", send(Socket, buf, bytesrecvd, 0)); > //don't want to do this every time! > //instead I want to do this.. > //data_to_consider += buf; > } > else break; //this break never occurs > } while(TRUE); > //here is where I want to send some data back, once and only, e.g. > send(Socket, ProcessAllData(data_to_consider), data_to_consider.length, > 0); > printf("\n"); > > _tprintf(_T("\nClient has disconnected\n")); > > does that make sense what I'm trying to get at? I'm not sure I follow what you're trying to do. An echo server's entire point is to echo data as it's received. If you're trying to implement some other protocol, the protocol description will tell you when you have received all the data (or enough to process). DS |
|
|||
|
Bonj wrote:
> Another problem perhaps you can advise me on... now I've got it working, > data is being sent back and forward reliably and the number of bytes sent is > always right. > However I've got a problem that's a bit more of a logic issue - I'll > describe what happens: the client connects to the server, and sends data > first. > The server recv()s this data successfully. > However I would like it so that the server can receive all the data in > multiple chunks (i.e. in a loop with a call to recv on each iteration)... > but the problem is knowing how to stop this loop. The bit I can't get my > head round is... the call to recv never returns 0 - it returns positive > integer while there is still data (which fills the buffer correctly) - but > then blocks indefinitely when all the data has been received... That's why it's called a blocking socket. You can get much better control of what's going on - and the ability to serve multiple clients - if you use a nonblocking socket. -- Scott McPhillips [VC++ MVP] |
|
|||
|
"David Schwartz" <davids@webmaster.com> wrote in message news:cvorcp$sb$1@nntp.webmaster.com... > > "Bonj" <a@b.com> wrote in message news:38a5ihF5lsvv2U1@individual.net... > >> However I've got a problem that's a bit more of a logic issue - I'll >> describe what happens: the client connects to the server, and sends data >> first. >> The server recv()s this data successfully. >> However I would like it so that the server can receive all the data in >> multiple chunks (i.e. in a loop with a call to recv on each iteration)... >> but the problem is knowing how to stop this loop. The bit I can't get my >> head round is... the call to recv never returns 0 - it returns positive >> integer while there is still data (which fills the buffer correctly) - >> but then blocks indefinitely when all the data has been received... >> *unless* - it sends some data back on each iteration, as in an echo >> server. But what if I want to receive all the data before formulating the >> response? > > Don't call 'recv' unless there's more data coming. Look at the data you > already got to see if there's more coming. Well, I'm beginning to think it might be easier to go down that route...however Scott's suggestion about a 'non-blocking socket' might have some mileage in it so I'll look into that. > >> I could think of one solution which is to designate a zero-terminator for >> the data. But this is slightly messy as it requires extra code, possibly >> with a loop, to detect this terminator byte. I don't want to send dummy >> data over the network as this is causing unnecessary traffic. > > Surely it's necessary to tell when you have all the data, isn't it? How > is that "unnecessary"? Well... (see next but 1 inline) > >> The code I've got for the server is: >> >> // std::string data_to_consider(""); >> char buf[RECVBUFLEN + 1]; >> do >> { >> int bytesrecvd = recv(Socket, buf, RECVBUFLEN, 0); >> Sleep(0); > > What's the Sleep for? See next inline... > >> if(bytesrecvd == SOCKET_ERROR) throw(5); >> if(bytesrecvd > 0) >> { >> buf[min(bytesrecvd, RECVBUFLEN)] = '\0'; >> printf("%s", buf); //in the non-demo application this would be >> probably appended to a std::string or something instead... >> printf("Sent %d bytes back\n", send(Socket, buf, bytesrecvd, 0)); >> //don't want to do this every time! >> //instead I want to do this.. >> //data_to_consider += buf; >> } >> else break; //this break never occurs >> } while(TRUE); >> //here is where I want to send some data back, once and only, e.g. >> send(Socket, ProcessAllData(data_to_consider), >> data_to_consider.length, 0); >> printf("\n"); >> >> _tprintf(_T("\nClient has disconnected\n")); >> >> does that make sense what I'm trying to get at? > > I'm not sure I follow what you're trying to do. What I'm essentially trying to do is to build a simple TCP server and client as a learning project - so in order to learn about how to listen, connect, send and receive data, etc, aswell as to learn about any differences there might be in the linux vs. windows implementations, and the differences between C and C# implementations - I decided to write a simple telnet server. The reason for writing it in C# is it has a quite natty Process class that I could use to pipe the stdout of the process back into the program (in order to send this back to the client), something I don't know how to do in C. The idea is a command could be any length - so to 'signal' the termination I would have to definitely send a zero byte and scan for it. It was working fine when the client was running on the same machine, using localhost as the server address. I say fine - for small amounts of data. For large amounts of data (e.g. if I sent "dir *" to the server then quite a lot of data would be returned - a service starts its life in c:\windows\system32 remember) it would receive a bit of it, then hang. If I *either* ran it under a debugger, or put Sleep(0) after the receive, it was fine. Thinking the C# 'telnet' server was working as it should (which I'm sure it is as you can use it in exactly the way I want from the linux machine) I started to write a client in C on linux, but then I ran into the problem that a remote machine couldn't connect. This problem plagued me for 2 days, and some of it involved me getting rid of my original (linux client) code and replacing it with some example code, thinking it might be at fault. This is a basic 'echo' client which sends one piece of data then quits. Now I've found the connection problem (which was that I had to listen on IPADDR_ANY - IPAddress.Any in C# rather than localhost - I previously thought this referred to the port the server was running on - doh, it knows that!) I can start to work out the blocking logic problem. However, I'm now slightly confused about: *whether it's possible to write some code in C (for windows or linux - I think the calls are pretty much the same) to have a 'conversation' with my C# telnet-server service (not just one stream of data transmitted then a stream returned then quit) *without* having to look for a null terminator in the data (and thus have to reprogram the C# service) *what some good (portable C) code would be to get input from the command line, and do the above? Would this involve setting the socket to 'non-blocking' do you think? The C# code for the service is this: void StartServer() { TcpListener server = new TcpListener(IPAddress.Any, PORT); //this was a PITA of a bug to fix... try { server.Start(); do { while(!server.Pending()) Thread.Sleep(500); ThreadPool.QueueUserWorkItem(new WaitCallback(this.AcceptRequest), server); //(new Thread(new ThreadStart(this.AcceptRequest))).Start(); evt.WaitOne(); } while(true); } catch(ThreadAbortException) {} finally{server.Stop();} } void AcceptRequest(object oServer) { try { TcpListener server = (TcpListener)oServer; TcpClient client; lock(server) {client = server.AcceptTcpClient();} evt.Set(); using(NetworkStream ns = client.GetStream()) using(ProcessRunner p = new ProcessRunner()) { bool processok = true; while(processok) { string s = ""; do { byte[] data = new byte[255]; int bytesgot = ns.Read(data, 0, 255); s = String.Concat(s, Encoding.ASCII.GetString(data, 0, bytesgot)); } while(ns.DataAvailable); //equivalent of this in C? if(!p.Auth) { if(s == password) { s = ""; p.Auth = true; } else { s = "Unauthorized"; processok = false; } } else { processok = p.GetResponse(ref s); } s += "NetCmd@" + p.CurDir + ">"; byte[] databack = Encoding.ASCII.GetBytes(s); ns.Write(databack, 0, databack.Length); } ns.Close(); client.Close(); } } catch {} } Basically "p", which is of type of a class called ProcessRunner I've designed, all it does is runs the process as an argument to "cmd /c", and returns the stdout.ReadToEnd as the return value. And the current client I've got (which works on linux but I'm sure would work on windows aswell) is as such int main(int argc, char *argv[]) { int sock; struct sockaddr_in echoserver; char buffer[BUFFSIZE]; unsigned int echolen; int received = 0; if (argc != 4) { fprintf(stderr, "USAGE: TCPecho <server_ip> <word> <port>\n"); exit(1); } /* Create the TCP socket */ if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { Die("Failed to create socket\n"); } /* Construct the server sockaddr_in structure */ memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */ echoserver.sin_family = AF_INET; /* Internet/IP */ echoserver.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */ echoserver.sin_port = htons(atoi(argv[3])); /* server port */ /* Establish connection */ if (connect(sock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0) { Die("Failed to connect with server\n"); } /* Send the word to the server */ echolen = strlen(argv[2]); if (send(sock, argv[2], echolen, 0) != echolen) { Die("Mismatch in number of sent bytes\n"); } /* Receive the word back from the server */ fprintf(stdout, "Received: "); while (received < echolen) { int bytes = 0; if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1) { Die("Failed to receive bytes from server\n"); } received += bytes; buffer[bytes] = '\0'; /* Assure null terminated string */ fprintf(stdout, buffer); } fprintf(stdout, "\n"); } but obviously that only sends one piece of data and then returns the result and quits. How can I engineer it to be a non-blocking socket to have a conversation with the above service as a telnet server? Thanks for the response! > An echo server's entire point is to echo data as it's received. If you're > trying to implement some other protocol, the protocol description will > tell you when you have received all the data (or enough to process). > > DS > > |