Server Error
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jas_singh
Posted On:   Saturday, April 7, 2007 04:00 PM

Hi, ive made a server for an instant messenger and created the server sockets, which was working fine, however when i attempted to use ssl (jsse) security i keep coming across the following error when running the code: Thread exceptionjava.net.BindException: Address already in use: JVM_Bind ERROR CLOSING SOCKET original server code: import java.util.*; import java.net.*; import java.io.*; import java.util.Vector; import java.net.InetAddress; import java.net.Socket; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList;    More>>

Hi, ive made a server for an instant messenger and created the server sockets, which was working fine, however when i attempted to use ssl (jsse) security i keep coming across the following error when running the code:
Thread exceptionjava.net.BindException: Address already in use: JVM_Bind
ERROR CLOSING SOCKET

original server code:

			
import java.util.*;
import java.net.*;
import java.io.*;
import java.util.Vector;

import java.net.InetAddress;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.util.ArrayList;



class Server implements ChatConstants
{
private static Vector list;
//private static Vector _userList;
private ServerSocket ssocket ;
private Service service;
private static Socket socket;
private boolean done=false;
private static Hashtable userTable = new Hashtable();
private static Hashtable _userList = new Hashtable();
private static ArrayList newUserList = new ArrayList();
private static Hashtable newHashList = new Hashtable();
private static Hashtable _conflist = new Hashtable();
public Server server;
public TimeTableDBModel tt;
Message message;
User user;
UserTreePanel utp;
Connection conn= null;

public Server() throws UnknownHostException
{
this.server = server;
System.out.println("Initializing...");
list=new Vector(BACKLOG);
//_userList=new Vector(BACKLOG);
try {
ssocket= new ServerSocket(SERVER_PORT,BACKLOG);
// System.out.println(list);

}
catch(Exception e) {
System.out.println("Inside constructor"+e);
}
start();
}



public void start() throws UnknownHostException
{

byte[] data;
int header;
Socket _socket = null;
String hostname = null;
System.out.println("Server successfully started at "
+InetAddress.getLocalHost().toString()
+" port "+SERVER_PORT);

while(!done) {
try {
_socket=ssocket.accept();
if(_socket != null) {
synchronized(list) {
list.addElement(_socket);
}

DataInputStream dis=new DataInputStream(_socket.getInputStream());
data = new byte[MAX_MESSAGE_SIZE];
dis.read(data);
Message message = ((Message)ChatUtils.bytesToObject(data));
System.out.println("Joined client "
+message._username+" at "+message._host+"...");


synchronized(userTable) {
userTable.put(message._username,_socket);


}



if(message._loguser.equals("User")){
addUser(message);
sendUserList(message);
}
if(message._loguser.equals("Admin")){
addAdmin(message);
sendUserList(message);

}


//writeToClients(message);
service = new Service(_socket,hostname,message._user);

}
System.out.println("NUMBER OF CLIENTS :"+list.size());


}

catch(Exception e) {
System.out.println("Thread exception"+e);
try {
_socket.close();
}
catch(Exception ex) {
System.out.println("ERROR CLOSING SOCKET");
}
}
}//END WHILE


}
}

revised server code:


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.net.*;
import java.io.*;
import java.io.*;
import javax.net.ssl.*;



import javax.swing.JOptionPane;

class Server implements ChatConstants
{
private static Vector list;
//private static Vector _userList;
private SSLServerSocket ssocket ;
private
Service service;
//private static Socket socket;
boolean done=false;
private static Hashtable userTable = new Hashtable();
private static Hashtable _userList = new Hashtable();
private static Hashtable _conflist = new Hashtable();
Connection conn = null;
Message message;
Server server;



public Server() throws UnknownHostException
{
System.out.println("Initializing...");
list=new Vector(BACKLOG);
//_userList=new Vector(BACKLOG);
try {
//ssocket= new SSLServerSocket(SERVER_PORT,BACKLOG);

SSLServerSocketFactory sslSrvFact = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
ssocket =(SSLServerSocket)sslSrvFact.createServerSocket(SERVER_PORT);

}
catch(Exception e) {
System.out.println("Inside constructor"+e);
}
start();
}



public void start() throws UnknownHostException
{

byte[] data;
int header;
SSLSocket _socket = null;
String hostname = null;
System.out.println("Server successfully started at "
+InetAddress.getLocalHost().toString()
+" port "+SERVER_PORT);


while(!done) {
try {
SSLServerSocketFactory sslSrvFact =
(SSLServerSocketFactory)
SSLServerSocketFactory.getDefault();
ssocket =(SSLServerSocket)sslSrvFact.createServerSocket(SERVER_PORT);




_socket=(SSLSocket)ssocket.accept();
if(_socket != null) {
synchronized(list) {
list.addElement(_socket);
}

DataInputStream dis=new DataInputStream(_socket.getInputStream());
data = new byte[MAX_MESSAGE_SIZE];
dis.read(data);
Message message = ((Message)ChatUtils.bytesToObject(data));
System.out.println("Joined client "
+message._username+" at "+message._host+"...");
synchronized(userTable) {
userTable.put(message._username,_socket);
}
addUser(message);
// sendUserList(message);
// writeToClients(message);
service = new Service(_socket,hostname,message._user);



}
//System.out.println("NUMBER OF CLIENTS :"+list.size());
}

catch(Exception e) {
System.out.println("Thread exception"+e);
try {
_socket.close();
}
catch(Exception ex) {
System.out.println("ERROR CLOSING SOCKET");
}
}
}//END WHILE

can someone please help its important that i solve this problem asap.
note- the port i am using SERVER_PORT = 443 and host is local. The original code worked and i am getting problems when i try implementing ssl.
Regards, Jas    <<Less
About | Sitemap | Contact