mirror of
https://github.com/brendanhaines/RasPi.git
synced 2024-11-09 16:44:40 -07:00
removes unnecessary TCP stuff
This commit is contained in:
parent
cbb7384889
commit
e1ad02ec67
12
TCP/Makefile
12
TCP/Makefile
|
@ -1,12 +0,0 @@
|
||||||
all: TCPserver.o TCPserverExample
|
|
||||||
|
|
||||||
HDRS = TCPserver.h
|
|
||||||
CXXFLAGS = -Wall
|
|
||||||
|
|
||||||
TCPserver.o TCPserverExample.o: $(HDRS)
|
|
||||||
|
|
||||||
TCPserverExample: TCPserver.o TCPserverExample.o
|
|
||||||
$(CXX) $^ -o $@
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm TCPserver.o TCPserverExample.o TCPserverExample
|
|
Binary file not shown.
|
@ -1,68 +0,0 @@
|
||||||
/**
|
|
||||||
* TCPclient.java
|
|
||||||
* Written by Brendan Haines
|
|
||||||
* Based on example from http://www.java-samples.com/showtutorial.php?tutorialid=1167
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.lang.*;
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.*;
|
|
||||||
|
|
||||||
class TCPclient {
|
|
||||||
/** Socket at the server */
|
|
||||||
private Socket socket;
|
|
||||||
/** buffer to hold received info */
|
|
||||||
private BufferedReader buffer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connects to socket at server
|
|
||||||
* @param host - the server's IP
|
|
||||||
* @param port - the port number of the socket at the server
|
|
||||||
*/
|
|
||||||
public TCPclient( String host, int port ) {
|
|
||||||
try {
|
|
||||||
System.out.println( "setting up socket" );
|
|
||||||
socket = new Socket( host, port );
|
|
||||||
System.out.println( "socket set up\nsetting up buffer" );
|
|
||||||
buffer = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
|
|
||||||
System.out.println( "buffer set up" );
|
|
||||||
}
|
|
||||||
catch( Exception e ) {
|
|
||||||
System.out.println( "ERROR -- constructor" );
|
|
||||||
close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* reads one line from the receive buffer
|
|
||||||
* @return one line of the receive buffer on success, null on failure
|
|
||||||
*/
|
|
||||||
public String readLine() {
|
|
||||||
try{
|
|
||||||
if( buffer.ready() ) {
|
|
||||||
return buffer.readLine();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch( Exception e ) {
|
|
||||||
System.out.println( "Hmmm... it won't read" );
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* close connection
|
|
||||||
*/
|
|
||||||
public void close() {
|
|
||||||
try {
|
|
||||||
buffer.close();
|
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
catch( Exception e ) {
|
|
||||||
System.out.println( "SH*T! the connection won't close" );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Binary file not shown.
|
@ -1,11 +0,0 @@
|
||||||
/**
|
|
||||||
* Written By Brendan Haines
|
|
||||||
*/
|
|
||||||
|
|
||||||
class TCPclientExample {
|
|
||||||
public static void main( String args[] ) {
|
|
||||||
TCPclient client = new TCPclient( "192.168.42.1", 51719 );
|
|
||||||
System.out.println( "Setup successful" );
|
|
||||||
client.close();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,134 +0,0 @@
|
||||||
/*
|
|
||||||
* TCPserver.cpp
|
|
||||||
* Written by Brendan Haines
|
|
||||||
* Based heavily on example code found at http://www.linuxhowtos.org/C_C++/socket.htm
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
#include "TCPserver.h"
|
|
||||||
|
|
||||||
TCPserver::TCPserver()
|
|
||||||
{}
|
|
||||||
|
|
||||||
// Constructor sets everything up and starts monitoring thread
|
|
||||||
TCPserver::TCPserver( int port )
|
|
||||||
{
|
|
||||||
portNumber = port;
|
|
||||||
setupSocket();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Destructor closes sockets
|
|
||||||
TCPserver::~TCPserver()
|
|
||||||
{
|
|
||||||
close( connectionFD );
|
|
||||||
close( socketFD );
|
|
||||||
}
|
|
||||||
|
|
||||||
// a unified/consistent way of reporting errors
|
|
||||||
void TCPserver::handleError( const char* message )
|
|
||||||
{
|
|
||||||
perror( message ); // print message to stderror
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns true on success, false on failure
|
|
||||||
bool TCPserver::setupSocket()
|
|
||||||
{
|
|
||||||
// Create a new socket
|
|
||||||
// AF_INET: internet domain (not unix domain)
|
|
||||||
// SOCK_STREAM: stream (not datagram)
|
|
||||||
// 0: lets OS decide correct protocol (TCP)
|
|
||||||
socketFD = socket( AF_INET, SOCK_STREAM, 0 );
|
|
||||||
if( socketFD < 0 ) // error setting up socket
|
|
||||||
{
|
|
||||||
handleError( "Error setting up socket\n" );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// fill server_addr with zeroes
|
|
||||||
// NOTE: memset sets arg2 bytes following arg0 to arg1
|
|
||||||
// NOTE: sizeof returns the size of the passed type in bytes
|
|
||||||
memset( (char*) &server_addr, 0, sizeof( server_addr ) );
|
|
||||||
|
|
||||||
server_addr.sin_family = AF_INET; // just following the example (and its documentation)
|
|
||||||
server_addr.sin_port = htons( portNumber ); // assign the port number. NOTE: htons converts host byte order to network byte order
|
|
||||||
server_addr.sin_addr.s_addr = INADDR_ANY; // INADDR_ANY represents the IP address of the host
|
|
||||||
|
|
||||||
// bind: binds the socket to an address
|
|
||||||
if( bind( socketFD, (struct sockaddr*) &server_addr, sizeof( server_addr ) ) < 0 )
|
|
||||||
{
|
|
||||||
handleError( "Error binding socket" );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// listen for connections to socket (max 5 waiting)
|
|
||||||
listen( socketFD, 5 );
|
|
||||||
|
|
||||||
clientAddrLen = sizeof( client_addr );
|
|
||||||
|
|
||||||
// wait for client to connect
|
|
||||||
printf( "Waiting for client to connect\n" );
|
|
||||||
connectionFD = accept( socketFD, (struct sockaddr*) &client_addr, &clientAddrLen );
|
|
||||||
if( connectionFD < 0 )
|
|
||||||
{
|
|
||||||
handleError( "Error on accept" );
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// empty buffer
|
|
||||||
memset( buffer, 0, sizeof( buffer ) );
|
|
||||||
|
|
||||||
return true; // setup successful
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns a pointer to the buffer
|
|
||||||
char* TCPserver::getBuffer()
|
|
||||||
{
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns the connection file descriptor
|
|
||||||
int TCPserver::getConnectionFD()
|
|
||||||
{
|
|
||||||
return connectionFD;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
void TCPserver::startMonitoringThread()
|
|
||||||
{
|
|
||||||
// create thread: ( thread, NULL = default values, function to run, parameter (void*) or NULL )
|
|
||||||
if( pthread_create( &monitorThread, NULL, monitoringThread, (void*) this ) )
|
|
||||||
{
|
|
||||||
// error creating thread
|
|
||||||
handleError( "Error creating thread" );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void* TCPserver::monitoringThread( void* serverPointer )
|
|
||||||
{
|
|
||||||
int n; // contains the number of characters read
|
|
||||||
|
|
||||||
TCPserver server;
|
|
||||||
|
|
||||||
while( true )
|
|
||||||
{
|
|
||||||
// read from the socket and put in the buffer
|
|
||||||
n = read( server.getConnectionFD(), server.getBuffer(), 255 );
|
|
||||||
if( n < 0 )
|
|
||||||
{
|
|
||||||
server.handleError( "Error reading -- monitorThread " );
|
|
||||||
pthread_exit( NULL ); // exit this thread
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
|
@ -1,42 +0,0 @@
|
||||||
/*
|
|
||||||
* TCPserver.h
|
|
||||||
* Written by Brendan Haines
|
|
||||||
* Based heavily on example code found at http://www.linuxhowtos.org/C_C++/socket.htm
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef TCP_SERVER_H
|
|
||||||
#define TCP_SERVER_H
|
|
||||||
|
|
||||||
#include <netinet/in.h>
|
|
||||||
|
|
||||||
|
|
||||||
class TCPserver {
|
|
||||||
public:
|
|
||||||
TCPserver();
|
|
||||||
TCPserver( int port ); // Constructor
|
|
||||||
~TCPserver(); // Destructor
|
|
||||||
|
|
||||||
void handleError( const char* message );
|
|
||||||
|
|
||||||
char* getBuffer();
|
|
||||||
int getConnectionFD();
|
|
||||||
|
|
||||||
char buffer[ 256 ]; // buffer to store read data
|
|
||||||
private:
|
|
||||||
int socketFD; // file descriptor for the socket
|
|
||||||
int connectionFD; // file descriptor for the connection
|
|
||||||
int portNumber; // port number of the socket
|
|
||||||
socklen_t clientAddrLen; // stores the length of the client's address
|
|
||||||
|
|
||||||
struct sockaddr_in server_addr; // contains the server's address
|
|
||||||
struct sockaddr_in client_addr; // contains the client's address
|
|
||||||
|
|
||||||
//pthread_t monitorThread; // thread to monitor the socket and read to buffer
|
|
||||||
|
|
||||||
bool setupSocket();
|
|
||||||
|
|
||||||
//void startMonitoringThread();
|
|
||||||
//void* monitoringThread( void* );
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
BIN
TCP/TCPserver.o
BIN
TCP/TCPserver.o
Binary file not shown.
Binary file not shown.
|
@ -1,11 +0,0 @@
|
||||||
/*
|
|
||||||
* TCPserverExample.cpp
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "stdio.h"
|
|
||||||
#include "TCPserver.h"
|
|
||||||
|
|
||||||
int main( int argc, char* argv[] )
|
|
||||||
{
|
|
||||||
TCPserver serv( 51719 );
|
|
||||||
}
|
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user