import java.io.*;
import java.net.*;
public class Server {
public static int i = -1;
public static void main(String args[])
{
int port = 6789;
Server server = new Server( port );
server.startServer();
}
// declare a server socket and a client socket for the server;
// declare the number of connections
ServerSocket BidServer = null;
Socket clientSocket = null;
int port;
public Server( int port ) {
this.port = port;
}
public void stopServer() {
System.out.println( "Server cleaning up." );
System.exit(0);
}
public void startServer() {
// Try to open a server socket on the given port
// Note that we can't choose a port less than 1024 if we are not
// privileged users (root)
try
{
BidServer = new ServerSocket(port);
}
catch (IOException e)
{
System.out.println(e);
}
System.out.println( "Server is started and is waiting for connections." );
System.out.println( "With multi-threading, multiple connections are allowed." );
System.out.println( "Any client can send -1 to stop the server." );
// Whenever a connection is received, start a new thread to process the connection
// and wait for the next connection.
while ( true )
{
try
{
clientSocket = BidServer.accept();
i++;
Server2Connection oneconnection = new Server2Connection(clientSocket, i, this);
new Thread(oneconnection).start();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
}
class Server2Connection implements Runnable
{
//String [] addr = new String [5];
//Socket [] addr = new Socket [5];
Socket [] addr = new Socket [5];
PrintStream [] os = new PrintStream [5];
BufferedReader [] is = new BufferedReader [5];
int i;
// BufferedReader is;
// PrintStream os;
Socket clientSocket;
//int id;
Server server;
public Server2Connection(Socket clientSocket, int i, Server server)
{
this.clientSocket = clientSocket;
//this.id = i;
this.server = server;
addr[i] = clientSocket;
this.i=i;
//System.out.println( "Connection " + id + " established with: " + clientSocket);
//System.out.println( "Address : " + clientSocket.getInetAddress());
//System.out.println( "Address with to string : " + clientSocket.getInetAddress().toString());
//addclient(addr,clientsocket,i);
//addr[i] = clientSocket;
//i++;
try
{
is[i] = new BufferedReader(new InputStreamReader(addr[i].getInputStream()));
os[i] = new PrintStream(addr[i].getOutputStream());
}
catch (IOException e)
{
System.out.println(e);
}
}
/*public void sendtoall(Socket [] addr,String line,PrintStream [] os,int i) //throws InterruptedException
{
int j=0;
for(j=0;j<=i;j++)
{
//System.out.println("\n" + line + "received from client 1");
System.out.println("Value of i in sendtoall " + i);
os[j].println("\n" + line + "received from client 1");
System.out.println("value of os [" + j + "] in sendtoall " + os[j]);
// delay(50);
}
/*try
{
wait(50);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}*/
/* private void delay(int j)
{
for(i=0;i<=j;i++);
}*/
public void run()
{
//Socket clientSocket = null;
String line;
//int j=0;
try
{
boolean serverStop = false;
while (true)
{
line = is[i].readLine();
System.out.println( "Received " + line + " from Connection " + i + "." );
int n = Integer.parseInt(line);
if ( n == -1 )
{
serverStop = true;
break;
}
if ( n == 0 ) break;
/* for(i=0;i<=addr.length;i++)
{
clientSocket = addr[i];
//os = new PrintStream(clientSocket.getOutputStream());
}*/
// System.out.println("Length = " + addr.length);
//for(j=0;j<=1;j++)
//{
//if(i!=j)
//{
System.out.println("Value of i : " + i);
System.out.println("Length of addr : " + addr.length);
//os[i].println("" + n*n );
// }
// }
// sendtoall(addr,line,os,i);
int j=0;
for(j=0;j<=i;j++)
{
//System.out.println("\n" + line + "received from client 1");
System.out.println("Value of i in sendtoall " + i);
os[j].println("\n" + line + "received from client 1");
System.out.println("value of os [" + j + "] in sendtoall " + os[j]);
// delay(50);
}
}
System.out.println( "Connection " + i + " closed." );
// is[i].close();
// os[i].close();
// clientSocket.close();
if ( serverStop )
server.stopServer();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class Client1
{
public static void main(String[] args)
{
String hostname = "192.168.1.68";
int port = 6789;
// declaration section:
// clientSocket: our client socket
// os: output stream
// is: input stream
Socket clientSocket = null;
DataOutputStream os = null;
BufferedReader is = null;
// Initialization section:
// Try to open a socket on the given port
// Try to open input and output streams
try
{
clientSocket = new Socket(hostname, port);
os = new DataOutputStream(clientSocket.getOutputStream());
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: " + hostname);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: " + hostname);
}
// If everything has been initialized then we want to write some data
// to the socket we have opened a connection to on the given port
if (clientSocket == null || os == null || is == null)
{
System.err.println( "Something is wrong. One variable is null." );
return;
}
try
{
while ( true )
{
System.out.print( "Enter an integer (0 to stop connection, -1 to stop server): " );
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String keyboardInput = br.readLine();
os.writeBytes( keyboardInput + "\n" );
int n = Integer.parseInt( keyboardInput );
if ( n == 0 || n == -1 ) {
break;
}
String responseLine = is.readLine();
System.out.println("Server returns its square as: " + responseLine);
}
// clean up:
// close the output stream
// close the input stream
// close the socket
os.close();
is.close();
clientSocket.close();
}
catch (UnknownHostException e)
{
System.err.println("Trying to connect to unknown host: " + e);
}
catch (IOException e)
{
System.err.println("IOException: " + e);
}
}
}
*Edited to add the correct [ /code] tags
This post has been edited by pbl: 30 November 2008 - 06:46 PM

New Topic/Question
Reply




MultiQuote




|