hello,
U have to run ur server program first in one
java console then run client program in new console
some points to remember...
ur server program should have to listen on a specific port in this program ur server should have to listen on port 80
but the more important thing is that ports above 1024 are for for system & are allocated automatically by Operating system.
so try to use port above 1024
here is some code which may help U
Sever Code
U may run it by changing the IP & port of ur desired machine
//________________________
import java.util.*;
import java.io.*;
import java.net.*;
public class SimpleChatServer extends
Thread {
private static final String SERVER_MSG_PREFIX = "SERVER";
private static final String CLIENT_MSG_PREFIX = "GETNEXTMSG";
private static final String ECHO = "ECHO";
protected java.util.Vector connections=null;
private String str=null;
public static void main(String argv[]) {
new SimpleChatServer();
}
public SimpleChatServer(){
int port = 6006;
ServerSocket ss = null;
try {
ss = new ServerSocket(port);
System.out.println("Server Socket Created Waiting for Clients...");
} catch (IOException e) {
System.out.println("Could not listen on port: " + port + ", " + e);
System.exit(1);
}
connections = new java.util.Vector();
System.out.println("Ready...");
while (true) {
Socket s = null;
try {
s = ss.accept();
System.out.println("Socket Created\n"+s);
}
catch (IOException e) {
System.out.println("Accept failed: " + port + ", " + e);
System.exit(1);
}
try {
BufferedReader read=new BufferedReader(new InputStreamReader(s.getInputStream()));
str = read.readLine();
System.out.println(str);
PrintWriter print=new PrintWriter(s.getOutputStream(),true);
print.println("Java Chat Service at port: 6006");
str = str.substring("GET /".length(),str.length()-" HTTP/1.1".length());
System.out.println(str);
if(str.startsWith(SimpleChatServer.CLIENT_MSG_PREFIX)){
synchronized(connections){
connections.addElement(new HttpConnection(s,s.getOutputStream(),s.getInputStream()));
}
System.out.println(connections);
continue;
}
if(str.startsWith(SimpleChatServer.ECHO)){
str = str.substring(SimpleChatServer.ECHO.length());
read.close();
print.close();
s.close();
for(int i = 0;i<connections.size();i++){>
HttpConnection connection = (HttpConnection)connections.elementAt(i);
synchronized(connections){
synchronized(connection){
connection.writeToConnection(str);
connection.writeClose();
connection.socketClose();
}
}
}
connections.removeAllElements();
System.out.println(connections);
continue;
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}