Posted to tcl by Cactapus at Sat Nov 17 18:04:06 GMT 2012view raw
- # Read data from a channel (the server socket) and put it to stdout
- proc read_sock {sock} {
- set l [gets $sock]
- puts stdout "ServerReply:$l"
- flush $sock
- }
-
- # open the connection to the echo server...
- set shost "localhost"
- set sport 5555
-
- # set up connection:
- set svrSock [socket $shost $sport]
-
- #if {[eof $svrSock]} { # connection closed}
-
- # Setup monitoring on the socket so that when there is data to be
- # read the proc "read_sock" is called
- fileevent $svrSock readable [list read_sock $svrSock]
-
- # configure channel modes
- # ensure the socket is line buffered so we can get a line of text
- fconfigure $svrSock -buffering line
-
- # message indicating connection accepted and we're ready to go
- puts "Connected to server"
-
- # wait for and handle either socket or stdin events...
- vwait eventLoop
-
- puts "Client Finished"
-
-
-
-
-
- ############### Java
- import java.io.*;
- import java.net.*;
- public class EchoClient{
- ServerSocket providerSocket;
- Socket connection = null;
- ObjectOutputStream out;
- ObjectInputStream in;
- String message;
-
- void connect(){
- try {
- //1. creating a server socket
- providerSocket = new ServerSocket(5555, 10);
- //2. Wait for connection
- System.out.println("Waiting for connection");
- connection = providerSocket.accept();
- System.out.println("Connection received from " + connection.getInetAddress().getHostName());
- //3. get Input and Output streams
- out = new ObjectOutputStream(connection.getOutputStream());
- //in = new ObjectInputStream(connection.getInputStream());
-
- out.flush();
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- void run(){
- // test message
- sendMessage(1 + ";" + 0 + ";" + 0 + ";" + 10 + ";");
- }
-
- void sendMessage(String msg)
- {
- try{
- out.writeChars(msg);
- out.flush();
-
- System.out.println("server> " + msg);
- //out.
- }
- catch(IOException ioException){
- ioException.printStackTrace();
- }
- catch(IllegalMonitorStateException e){
- e.printStackTrace();
- }
- }
- public static void main(String args[])
- {
-
- EchoClient server = new EchoClient();
- server.connect();
- while(true){
- server.run();
- }
- }
- }