User Tools

Site Tools


user:mtaft4:portfolio:providerclass

This is code for a Java based server to send a series of predefined messages back and forth before shutting down. The comments will contain sort of beginning how this works comments to help people from the class understand the code.

import java.io.*;  //Importing the I/O and ServerSocket packages in order to use the 
import java.net.ServerSocket; //namespaces for printing messages and creating sockets
public class Provider{ 
	ServerSocket providerSocket;  //Instantiate the ServerSocket object
	Socket connection = null;     //No connection yet
	ObjectOutputStream toClient;  //Streams to print and send messages by
	ObjectInputStream fromClient;
	String message;  //The actual message being sent
	Provider(){}  //Somewhat function prototype
	void run() {  //Method for actually creating sockets and sending messages
		try {
			providerSocket = new ServerSocket(2004, 10); //Create a socket on port 2004 to accept connections from the client
			System.out.println("Waiting for connection"); 
			connection = providerSocket.accept(); //listen for a connection
			System.out.println("Connection received from " + connection.getInetAddress().getHostName());
			toClient = new ObjectOutputStream(connection.getOutputStream()); //create output object 
			toClient.flush(); //flush the buffer
			in = new ObjectInputStream(connection.getInputStream()); //create input object
			sendMessage("Connection successful");
			do { //loop while there's a connection
				try { //do the messaging
					message = (String)in.readObject();
					System.out.println("client>" + message);
					if (message.equals("bye"))
						sendMessage("bye");
				}
				catch(ClassNotFoundException classnot) { //catch the exceptions that the try can throw
					System.err.println("Data received in unknown format");
				}
			} while (!message.equals("bye"));
		}
		catch(IOException ioException) { //catch exceptions
			ioException.printStackTrace();
		}
		finally {
			try { //close all connections
				in.close();
				out.close();
				providerSocket.close();
			}
			catch(IOException ioException) {
				ioException.printStackTrace();
			}
		}
	}
	void sendMessage(String msg) { //sending messages to the client
		try {
			out.writeObject(msg);
			out.flush();
			System.out.println("server>" + msg);
		}
		catch(IOException ioException) {
			ioException.printStackTrace();
		}
	}
	public static void main(String args[]) {  //Static method to instantiate the class we created 
		Provider server = new Provider();
		while(true) {
			server.run();
		}
	}
}

The code is a little choppy currently as this was written with limited knowledge of Java and pretty much straight from the Java documentation. The next step is to either get this to send user defined messages or to send midi notes back and forth. Hopefully a it stands the client/server code should be able to become a base for both the Jello and Orka projects.

user/mtaft4/portfolio/providerclass.txt · Last modified: 2011/03/22 11:18 by mtaft4