[2618] in java-interest
ServerSocket
daemon@ATHENA.MIT.EDU (sauth103@morgan.com)
Fri Oct 6 13:29:03 1995
From: sauth103@morgan.com
Date: Fri, 6 Oct 95 11:06:25 EDT
To: java-interest@java.sun.com, rinaldo.digiorgio@East.Sun.COM
The program below is compiled started with java QuoteServermain. When a client
attempts to connect to it using "localhost" as the host name. We are refused
the connection. When the real host name is used the connection is granted.
import java.net.*;
import java.io.*;
//
// Open a Socket on the server and send a price for the given stock
// read is done on this socket. Use localhost and port 7000
//
class QuoteServerMain {
public static void main(String args[]) {
byte b[] = new byte [100];
String response;
int slength;
Double price;
ServerSocket s;
Socket s1;
int nbytes;
int PortNumber = 7000;
InputStream inputStream;
OutputStream outputStream;
try {
s = new ServerSocket(PortNumber,20000);
} catch(Exception e) {
System.out.println("Unable to create socket " + PortNumber);
return;
}
// Look for requests for data constantly
while ( true ) {
try {
s1 = s.accept();
inputStream = s1.getInputStream();
outputStream = s1.getOutputStream();
nbytes = inputStream.read(b,0,10); // Read the input
response = new String(b,0,0,nbytes);
System.out.println(response);
price = new Double(Math.random() * 100.0);
String one = new String(price.toString()); // Make up a price
slength = one.length();
one.getBytes(0,slength,b,0);
outputStream.write(b,0,slength);
s1.close();
} catch(Exception e) {
System.out.println("Unable to create socket " + PortNumber);
return;
}
}
}
}
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com