[5273] in java-interest
Client Server Socket Testing
daemon@ATHENA.MIT.EDU (Leitheiser)
Sun Feb 4 17:44:46 1996
Date: Sun, 4 Feb 1996 15:20:37 -0600
From: jdleithe@ii1.mke.ab.com (Leitheiser)
To: java-interest@java.sun.com, jdleithe@ii1.mke.ab.com
Hi,
I'm new to java, but experienced in other OO languages.....
My first question deals with the interface java.io.DataOutput/Input
... in the writeUTF() method....where is the spec for what a UTF format is, and what is the % overhead for these higher level IO methods?. vs individual chars
at the stream level?
My second Question is with the following server and client code....(JDK-1.0 )
They both work well on Solaris, but running on win95 it locks up ( 96 client messages, 1 server message ). Is this a bug/limitation on win95 or an error
in the code for garbage collection?
Thanks!
Jeff leitheiser
Rockwell Automation ( Allen-Bradley )
SERVER SOURCE:
// TCP/IP Example - Server
import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.*;
public class simpleServer extends Applet implements Runnable {
boolean inApplet = true;
ButtonQuit bQuit;
TextField fWhat;
TextField fStatus;
StringBuffer sendString = new StringBuffer("Hello Net World!\n" );
static Frame f = null;
static simpleServer mySpace = null;
Thread kicker = null;
public static void main(String args[]) throws IOException {
f = new Frame( "Test" );
mySpace = new simpleServer();
mySpace.init();
f.add( "Center", mySpace );
f.pack();
f.resize( f.preferredSize());
f.show();
mySpace.inApplet = false;
}
public void init() {
Panel bottomPanel = new Panel();
fWhat = new TextField( sendString.toString(), 20 );
fStatus = new TextField( "Initial_Value", 20 );
bQuit = new ButtonQuit( );
bottomPanel.add( fWhat );
bottomPanel.add( fStatus );
bottomPanel.add( bQuit );
setLayout( new BorderLayout());
add( "South", bottomPanel );
kicker = new Thread( this );
if( kicker != null ) {
kicker.start();
}
}
public void run() {
// Create service on server
ServerSocket s = null;
Socket s1 = null;
int cnt = 0;
int slength;
OutputStream s1out;
boolean soketReady = true;
try {
s = new ServerSocket(4321,300);
} catch (IOException e) {
fStatus.setText( "error opening" );
repaint();
soketReady = false;
}
// Run the listen/accept loop forever
while ( soketReady == true) {
sendString.setLength( 0 );
if( fWhat.getText() != "" ) {
sendString.append( fWhat.getText() );
try {
// Wait here and listen for a connection
fStatus.setText( "socket Ready! "+ cnt );
repaint();
s1=s.accept();
fStatus.setText( "socket found!" );
repaint();
// Get an output file handle associated with the socket
s1out = s1.getOutputStream();
// Send our string!
slength = sendString.length();
for (int i=0; i<slength; i++) {
s1out.write((int)sendString.charAt(i));
}
// Close the connection, but not the server socket
cnt++;
s1.close();
} catch (IOException e) {
fStatus.setText( "error running" );
repaint();
} // end fo try
} else {
// sendString.append( "?" );
System.out.print( "Empty String!\n" );
} // end of empty string
} // end of loop
}
public void stop() {
fWhat.setText( "???" );
repaint();
}
} // end of class
class ButtonQuit extends Button {
public ButtonQuit( ) {
super( "Quit" );
setBackground( Color.red );
setForeground( Color.white );
}
public boolean action( Event evt, Object arg ) {
boolean retStatus = true; // event is processed
postEvent( new Event( this, Event.WINDOW_DESTROY, this ) );
return( retStatus );
}
} // end of class
CLIENT SOURCE:
// TCP/IP Example - Client
import java.applet.*;
import java.io.*;
import java.net.*;
import java.awt.*;
public class simpleClient extends Applet implements Runnable {
boolean inApplet = true;
ButtonQuit bQuit;
TextField fWhat;
TextField fStatus;
StringBuffer msgString = new StringBuffer( 300 );
static Frame f = null;
static simpleClient mySpace = null;
Thread kicker = null;
public static void main(String args[]) throws IOException {
f = new Frame( "Test" );
mySpace = new simpleClient();
mySpace.init();
f.add( "Center", mySpace );
f.pack();
f.resize( f.preferredSize());
f.show();
mySpace.inApplet = false;
}
public void init() {
Panel bottomPanel = new Panel();
fWhat = new TextField( "", 20 );
fStatus = new TextField( "Initial_Value", 20 );
bQuit = new ButtonQuit( );
bottomPanel.add( fWhat );
bottomPanel.add( fStatus );
bottomPanel.add( bQuit );
setLayout( new BorderLayout());
add( "South", bottomPanel );
kicker = new Thread( this );
if( kicker != null ) {
kicker.start();
}
}
public void run() {
// Create service on server
Socket s = null;
int cnt = 0;
int slength;
InputStream sIn;
int c;
boolean socketReady = true;
// Open our connection to gypsy_i, at port 4321
while( true ) {
try {
s = new Socket("iipc2_i",4321);
} catch (IOException e1) {
fStatus.setText( "error opening,sleep 10" );
repaint();
socketReady = false;
try {
Thread.sleep( 10000, 0 );
} catch (InterruptedException e) {
break;
}
continue;
} // end of try
// Run the listen/accept loop forever
try {
msgString.setLength( 0 );
// Wait here and listen for a connection
fStatus.setText( "socket Ready!" );
repaint();
// Get an input file handle from the socket and read the input
sIn = s.getInputStream();
while ((c = sIn.read()) != -1) {
// System.out.print((char)c);
msgString.append( (char)c );
}
// When the EOF is reached, just close the connection and exit
fWhat.setText( msgString.toString() );
repaint();
s.close();
fStatus.setText( "socket done!" );
} catch (IOException e) {
fStatus.setText( "error running" );
repaint();
} // end fo try
} // end of loop
} // end of method
/***/
public void stop() {
fWhat.setText( "???" );
repaint();
}
} // end of class
class ButtonQuit extends Button {
public ButtonQuit( ) {
super( "Quit" );
setBackground( Color.red );
setForeground( Color.white );
}
public boolean action( Event evt, Object arg ) {
boolean retStatus = true; // event is processed
postEvent( new Event( this, Event.WINDOW_DESTROY, this ) );
return( retStatus );
}
} // end of class
**** end ****
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com