Tuesday, October 6, 2009

java socket comms

/**
* SimpleSocketClient.java
*
* Created on October 2, 2002, 5:34 PM
*/

import java.awt.*;
import java.net.*; //contains socket class
import java.io.*; //The java.io package provides input and output classes that we will use to extract the data from the response

import java.applet.Applet;


/**
*
* @author Stephen Potts
*/
public class AppletSocketClient // extends Applet

{
Socket socket1;
int portNumber = 20000;
String str = "";


ObjectInputStream ois;

InputStream istream;
OutputStream ostream;

byte[] theByteArray;

/** Creates a new instance of AppletSocketClient */
public AppletSocketClient()
{



try
{


// socket1 = new Socket(InetAddress.getLocalHost(), portNumber);

socket1 = new Socket("192.168.0.201", portNumber);

// socket1 = new Socket("192.168.0.201", portNumber,"192.168.0.1", 8000);

System.out.println("--Debug: build socket ");

istream=socket1.getInputStream();

// ois = new ObjectInputStream(istream);

System.out.println("--Debug: build input stream ");

ostream=socket1.getOutputStream();

// ObjectOutputStream oos = new ObjectOutputStream(socket1.getOutputStream());

System.out.println("--Debug: build output stream ");

str = "initialize";

System.out.println("--Debug: about to send string "+str);

theByteArray=str.getBytes();

ostream.write(theByteArray);

// oos.writeObject(str);

// System.out.println("--Debug: out string"+str);;
//
// str = (String) ois.readObject();
//
// System.out.println("--Debug: input string"+str);
//
// oos.writeObject("bye");



// while ((str = (String) ois.readObject()) == null);

// while ((str = (String) ois.readObject()) != null)
// {
// System.out.println(str);
// oos.writeObject("bye");
//
// if (str.equals("bye bye"))
// break;
// }

System.out.println("--Debug:ois close, oos close, socket close");

// ois.close();
// oos.close();
socket1.close();

} catch (Exception e)
{
System.out.println("Exception " + e);
}
}

public void paint(Graphics g)
{
g.drawString("Hello, Applet", 10, 10);
}


public static void main(String args[])
{
System.out.println("--Debug: main start ");

AppletSocketClient lsp = new AppletSocketClient();
}

}



++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/*
* LoopingSocketServer.java
*
* Created on October 2, 2002, 5:24 PM
*/

import java.net.*;
import java.io.*;

/**
*
* @author Stephen Potts
*/
public class AppSocketServer
{
ServerSocket servSocket;
Socket fromClientSocket;
int cTosPortNumber = 20000;
String str;


/** Creates a new instance of LoopingSocketServer */
public AppSocketServer()
{
// Create ServerSocket to listen for connections
try
{
servSocket = new ServerSocket(cTosPortNumber);

// Wait for client to connnect, then get Socket
System.out.println("ServerSocket created");
System.out.println("Waiting for a connection on " + cTosPortNumber);

fromClientSocket = servSocket.accept();
System.out.println("fromClientSocket accepted");

// Use ObjectOutputStream to send String to the client
ObjectOutputStream oos =
new ObjectOutputStream(fromClientSocket.getOutputStream());

//Use ObjectInputStream to get String from client
ObjectInputStream ois =
new ObjectInputStream(fromClientSocket.getInputStream());


while ((str = (String) ois.readObject()) != null)
{
System.out.println("The message from client is *** " + str);

if (str.equals("bye"))
{
oos.writeObject("bye bye");
break;
}
else
{
str = "Server returns " + str;
oos.writeObject(str);
}

}
oos.close();

// Close Sockets
fromClientSocket.close();
} catch (Exception e)
{
System.out.println("Exception " + e);
}
}

public static void main(String args[])
{
AppSocketServer lss = new AppSocketServer();
}


}

No comments: