digitalmars.D.learn - Sending messages using socket
- nrgyzer (23/23) Jul 24 2011 Hi guys,
- Adam Ruppe (18/18) Jul 24 2011 Browsers speak HTTP, which is a higher level protocol than
Hi guys, I'm trying to work with sockets but I've some trouble with them. I implemented a really simple Socket which should simply send a text message after accepting the request. My code is the following: module server; import std.socket; void main() { TcpSocket server = new TcpSocket(); server.bind(new InternetAddress(8080)); server.listen(1); while(true) { Socket cs = server.accept(); cs.send("test"); } } When I start my small sample application and tries to connect (http:// localhost:8080) to them using a browser I don't get any message. It seems to work forever and sometimes I get my message "test", but the connection between my browser and my application won't finished. When I add cs.close() after cs.send("test"), the connection will be closed, but I don't get any message in my browser - the browser says that the service isn't available (error-page). What's wrong with my code? => Thanks for any help :)
Jul 24 2011
Browsers speak HTTP, which is a higher level protocol than plain sockets. If you connect to your app with a simpler program, like nc, you'll probably see the message. If you want to serve web pages, consider one of these options: a) Looking up the HTTP protocol itself. You've gotta send headers before you send data or the browser won't understand what you are replying to. b) Use an existing web server, like Apache, and write your app as a cgi program The HTTP headers you need to send on a plain socket look like this: ===== HTTP/1.0 200 OK Content-Length: 11 Content-Type: text/html hello world ===== instead of just plain "hello world".
Jul 24 2011
== Auszug aus Adam Ruppe (destructionator gmail.com)'s ArtikelBrowsers speak HTTP, which is a higher level protocol than plain sockets. If you connect to your app with a simpler program, like nc, you'll probably see the message. If you want to serve web pages, consider one of these options: a) Looking up the HTTP protocol itself. You've gotta send headers before you send data or the browser won't understand what you are replying to. b) Use an existing web server, like Apache, and write your app as a cgi program The HTTP headers you need to send on a plain socket look like this: ===== HTTP/1.0 200 OK Content-Length: 11 Content-Type: text/html hello world ===== instead of just plain "hello world".Great - works, thanks :)
Jul 24 2011
== Auszug aus Adam Ruppe (destructionator gmail.com)'s ArtikelBrowsers speak HTTP, which is a higher level protocol than plain sockets. If you connect to your app with a simpler program, like nc, you'll probably see the message. If you want to serve web pages, consider one of these options: a) Looking up the HTTP protocol itself. You've gotta send headers before you send data or the browser won't understand what you are replying to. b) Use an existing web server, like Apache, and write your app as a cgi program The HTTP headers you need to send on a plain socket look like this: ===== HTTP/1.0 200 OK Content-Length: 11 Content-Type: text/html hello world ===== instead of just plain "hello world".The message exchange works, but I've some trouble in getting the messages correctly. I've the following, small apps: module server; import std.socket; import std.stream; import std.socketStream; void main() { TcpSocket s = new TcpSocket(); s.bind(new InternetAddress(5088)); s.listen(1); while(true) { Socket a = s.accept(); SocketStream cs = new SocketStream(a); string response = "HELLO WORLD!"; cs.writeBlock(response.ptr, response.length); a.close(); } } module client; import std.socket; import std.math; import std.stream; import std.socketStream; import std.stdio : writeln; void main() { TcpSocket s = new TcpSocket(); s.connect(new InternetAddress("127.0.0.1", 5088)); while(true) { void[] msg; SocketStream cs = new SocketStream(s); cs.readBlock(msg.ptr, 12); writeln(msg); } } But... when I start the server and client, I don't get any response/and or my message is empty. When I use the simply methods of streams (like write()), it works but the problem is, that the following will end in an endless loop: module client; import std.socket; import std.math; import std.stream; import std.socketStream; import std.stdio : writeln; void main() { TcpSocket s = new TcpSocket(); s.connect(new InternetAddress("127.0.0.1", 5088)); while(true) { uint len; string msg; SocketStream cs = new SocketStream(s); while(!cs.eof()) { // this loop repeats forever, although the stream is empty cs.read(len); cs.readString(msg, len); } } }
Jul 25 2011