Docstoc

Server

Document Sample
Server Powered By Docstoc
					// Server chat program
import java.net.*;
import java.io.*;
import java.util.*;

public class Server {
public static void main(String[] ar) {
 int port = 6666; //random port
 try {
    ServerSocket ss = new ServerSocket(port); // create a server socket
    System.out.println("Waiting for a client...");
 Socket socket = ss.accept(); // make the server listen for a connection,
 System.out.println("Got a client :) .!");
    System.out.println();

    InputStream sin = socket.getInputStream();
    OutputStream sout = socket.getOutputStream();

    DataInputStream in = new DataInputStream(sin);
    DataOutputStream out = new DataOutputStream(sout);

Scanner st=new Scanner(System.in);
 String n = null;
    while(true) {
 n = in.readUTF(); // wait for the client to initiate chat
 System.out.println("The peer sent : " + n);
 System.out.println("Please reply...");

n=st.nextLine();
    out.writeUTF(n);
    out.flush();
 System.out.println("Waiting for peer...");

}
}
  catch(Exception x)
{
x.printStackTrace();
}
}
}
/*
Sample Run for server side:
C:\Users\admin\Desktop>javac Server.java

C:\Users\admin\Desktop>java Server
Waiting for a client...
Got a client :) .!

The peer sent : This is D12...
Please reply...
I am a member of this forum too!
Waiting for peer...
The peer sent : Then welcome to D12...bye.
Please reply...
bye
Waiting for peer...
*/

				
DOCUMENT INFO
Shared By:
Tags: peer, peer, chat
Stats:
views:5
posted:6/27/2012
language:Swedish
pages:2
Description: chat