Created
October 14, 2011 05:25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
public class MyEchoClient { | |
public static void main(String args[]) { | |
try { | |
Socket sock = new Socket("127.0.0.1" , 10007); | |
InputStream in = sock.getInputStream(); | |
OutputStream out = sock.getOutputStream(); | |
String str = "hello\n"; | |
out.write(str.getBytes()); | |
out.flush(); | |
byte[] buf = new byte[1024]; | |
int n = in.read(buf); | |
System.out.write(buf, 0, n); | |
sock.close(); | |
} catch (Exception e) {} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import socket | |
def _send_message(host, port, message): | |
sock = socket.create_connection((host, port)) | |
sock.sendall(message) | |
resp = sock.recv(1024) | |
sock.close() | |
print 'Response: %s' % resp | |
if __name__ == '__main__': | |
host = '' | |
port = 10007 | |
message = 'hi' | |
_send_message(host, port, message) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import SocketServer | |
class MyHandler(SocketServer.BaseRequestHandler): | |
def handle(self): | |
self.data = self.request.recv(1024) | |
print '"%s" connected, and sent "%s"' % \ | |
(self.client_address[0], self.data) | |
self.request.send(self.data.upper()) | |
if __name__ == '__main__': | |
host = 'localhost' | |
port = 10007 | |
server = SocketServer.TCPServer((host, port), MyHandler) | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment