RasPi/CopterController/TCPexample/Server.java

50 lines
1.4 KiB
Java
Raw Normal View History

2015-01-20 11:17:36 -07:00
import java.lang.*;
import java.io.*;
import java.net.*;
/**
* Echo server. if receives "DISCONNECT", will shut down server properly
2015-02-09 14:58:01 -07:00
* Used for testing GroundStation communications
2015-01-20 11:17:36 -07:00
*/
class Server {
public static void main(String args[]) {
ServerSocket srvr;
Socket skt;
BufferedReader in;
PrintWriter out;
try {
System.out.print( "Setting up server\n" );
srvr = new ServerSocket( Integer.parseInt( args[0] ) );
System.out.println( srvr.getInetAddress() );
System.out.println( srvr.getLocalPort() );
2015-01-20 11:17:36 -07:00
skt = srvr.accept();
System.out.print( "Server has connected!\n" );
in = new BufferedReader( new InputStreamReader( skt.getInputStream() ) );
out = new PrintWriter( skt.getOutputStream(), true);
while( true ) {
System.out.print( "waiting for message...\n" );
while( !in.ready() ) {}
String rx = in.readLine();
System.out.print( "message received: " + rx + "\n" );
if( rx.equals( "DISCONNECT" ) ) {
System.out.print( "disconnecting\n" );
break;
}
System.out.print("Sending string: '" + rx + "'\n");
out.println( rx );
}
out.close();
in.close();
skt.close();
srvr.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}