mirror of
https://github.com/brendanhaines/RasPi.git
synced 2025-04-18 23:08:04 -06:00
adds TCP/IP example code
This commit is contained in:
parent
61ab00cc4e
commit
5daf0cf61e
BIN
TCPexample/Client.class
Normal file
BIN
TCPexample/Client.class
Normal file
Binary file not shown.
23
TCPexample/Client.java
Normal file
23
TCPexample/Client.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import java.lang.*;
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
class Client {
|
||||||
|
public static void main(String args[]) {
|
||||||
|
try {
|
||||||
|
Socket skt = new Socket("192.168.42.1", 1234);
|
||||||
|
BufferedReader in = new BufferedReader(new
|
||||||
|
InputStreamReader(skt.getInputStream()));
|
||||||
|
System.out.print("Received string: '");
|
||||||
|
|
||||||
|
while (!in.ready()) {}
|
||||||
|
System.out.println(in.readLine()); // Read one line and output it
|
||||||
|
|
||||||
|
System.out.print("'\n");
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
catch(Exception e) {
|
||||||
|
System.out.print("Whoops! It didn't work!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
TCPexample/Server.class
Normal file
BIN
TCPexample/Server.class
Normal file
Binary file not shown.
47
TCPexample/Server.java
Normal file
47
TCPexample/Server.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import java.lang.*;
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Echo server. if receives "DISCONNECT", will shut down server properly
|
||||||
|
*/
|
||||||
|
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( 51717 );
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Binary file not shown.
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
// From:
|
// From:
|
||||||
// http://www.linuxhowtos.org/C_C++/socket.htm
|
// http://www.linuxhowtos.org/C_C++/socket.htm
|
||||||
|
// commented for reference
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user