| PLEX86 | ||
|
TCP connection refused 4260
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. Dual head, equalize color difference Michael Heiming The ATI drivers only support the 8500 and higher cards, so it's no surprise they didn't work... Well... (see next but 1 inline) See next inline... 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 clbutt 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 IPADDRANY - 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();} } Intel to cut Linux out of the content market 4262 Stan Goodman Because Microsoft isn't going to roll over and play dead. I think these are the major blind... 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 byte255; 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 == pbuttword) { s = ; p.Auth = true; } else { s = "Unauthorized"; processok = false; } } else { processok = p.GetResponse(ref s); } 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 clbutt called ProcessRunner I've designed, all it does is runs the process as an argument to "cmdc", 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 sockaddrin echoserver; char bufferBUFFSIZE; unsigned int echolen; int received = 0; if (argc != 4) { exit(1); } * Create the TCP socket * Die("Failed to create socket-n"); } Intel to cut Linux out of the content market 4261 Sleep well tonight. Predicated on the uneasiness of "Dan" who had been taught since childhood that it is wrong to share books", the example is, was, and will remain ridiculous. (The only reason I... * Construct the server sockaddrin structure * memset(&echoserver, 0, sizeof(echoserver)); * Clear struct * echoserver.sinfamily = AFINET; * Internet-IP * echoserver.sinaddr.saddr = inetaddr(argv1); * IP address * echoserver.sinport = htons(atoi(argv3)); * server port * * Establish connection * if (connect(sock, (struct sockaddr *) &echoserver, Die("Failed to connect with server-n"); } * Send the word to the server * echolen = strlen(argv2); if (send(sock, argv2, echolen, 0) != echolen) { Die("Mismatch in number of sent bytes-n"); } * Receive the word back from the server * fprintf(stdout, "Received: "); int bytes = 0; Die("Failed to receive bytes from server-n"); } received += bytes; bufferbytes = '-0'; * butture 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!
|
||||
Intel to cut Linux out of the content market 4261 Linux groups from Newsgroups The #1 Usenet Provider on the Internet
|
||||