Currently I have my tcp version of the socket in this form.
final ServerSocket serverSocketConn = new ServerSocket(9000);
while (true)
{
try
{
Socket socketConn1 = serverSocketConn.accept();
new Thread(new ConnectionHandler(socketConn1)).start();
}
catch(Exception e)
{
System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
e.printStackTrace(System.out);
}
}
class ConnectionHandler implements Runnable {
private Socket receivedSocketConn1;
ConnectionHandler(Socket receivedSocketConn1) {
this.receivedSocketConn1=receivedSocketConn1;
}
try{
BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(receivedSocketConn1.getOutputStream()));
BufferedReader bReader = new BufferedReader(new InputStreamReader(receivedSocketConn1.getInputStream()));
}
}
It works fine so I am in the midst of converting it into UDP.
public class commUDP9000 {
class ReceiverThread implements Runnable {
private DatagramSocket receivedSocketConn1;
ReceiverThread(DatagramSocket receivedSocketConn1) {
System.out.println("Thread Received");
this.receivedSocketConn1=receivedSocketConn1;
}
public void run(){
while (true){
try{
//
final byte[] buffer = new byte[1024];
final DatagramPacket receivePacket = new DatagramPacket(buffer, buffer.length);
receivedSocketConn1.receive(receivePacket);
String sentence = new String( receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
}
catch(Exception e){
System.out.println("MyError:Socket Accepting has been caught in main loop."+e.toString());
e.printStackTrace(System.out);
}
}
}
}
public static void main(String[] args) {
new commUDP9000();
}
commUDP9000() {
try{
final DatagramSocket serverSocketConn = new DatagramSocket (9000);
new Thread(new ReceiverThread(serverSocketConn)).start();
}
catch (Exception e)
{
System.out.println("MyError:Socket Conn has been caught in main loop."+e.toString());
e.printStackTrace(System.out);
//System.exit(0);
}
}
}
I run this and end up with
MyError:Socket Accepting has been caught in main loop.java.net.BindException: Address already in use