Question

Hi I'm new to java socket and I've been trying to create a server socket. Can someone please tell me where I'm going wrong because I'm getting an error saying "Address already in use". Sometimes I don't get an error but instead it just doesn't form a connection. Help please? Here's the server class and the client class.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;


public class MessageServer {
public static void main(String[] args) throws IOException{

    int port = 8080;

    //int port = Integer.parseInt(args[0]); 

    ServerSocket server = new ServerSocket(port);


    while(true){
        System.out.println("Waiting for client...");
        Socket client = server.accept();

        System.out.println("Client from " + client.getInetAddress() + " connected.");
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String inputLine = in.readLine();
        System.out.println("Client said: '"+inputLine+"'");
        Writer count = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        byte c [] = count.toString().getBytes();
        count.flush();


        }
}

}

import java.net.*;
import java.io.*;

public class MessageSendClient {

public static void man(String args[]) throws IOException{
    Socket server1 = new Socket("143.210.72.82", 8080);
    System.out.println("Connected to " + server1.getInetAddress());
    InputStream in = server1.getInputStream();

    byte c[] = new byte[100];
    int num = in.read(c);
    String count = new String(c);

    System.out.println("Server said: " + count);
}

}

Was it helpful?

Solution

if you are in Linux 8080 / 80 / 8443 are no good ports us an other port like 3456. And always always Close your socket from your Client.

Update: first of all, you should not Close the Server socket with Server.close(), Server should be running. and thats why the while Loops is there. you should Close your Client socket, with socket.close() after you have get the inputstream from Server. Remember also to always Close your outputstream with Close() after your write() and flush() to avoid side effect. Make two class, one for the Server and one for the Client. Start Java class Server first, and then run your Client class to connect to Server. terminate your Client class and run it again. if you are using eclipse, also terminate the Client in your console. sometimes the Java processes are overlaping, so just terminate all the running process with the x button. After you have improved your example codes, you can start Server and Client class again. If it is still not working, you need to repost your question again and Show your current codes in Details for the Client and Server. somehow i have lost the track, where is your current state, cause you have already approved the answer to your question.

OTHER TIPS

don't use your ip but use localhost or 127.0.0.1 and also try a new port, try to use a port greater than 1024 (most under this number are well known port and they're registred to IANA)

Try a new port. I wouldn't use 8080 since it may be (and probably is already used). Also try to always close the socket after you used it.

Since I used sockets myself for university purposes, here is the code (if you need UDP or RMI I can post them as well)

public class TCPClient {
  public static void main(String[] args){
    try{
      Socket socket = new Socket("127.0.0.1", 6666);

      PrintWriter data_to_server = new PrintWriter(socket.getOutputStream(), true);
      BufferedReader data_from_server =
              new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));

      //Schicke Nachricht an Server
      BufferedReader system_reader = new BufferedReader(new InputStreamReader(System.in));
      long start_send = System.nanoTime();
      String msg = system_reader.readLine();
      data_to_server.println(msg);

      //Erhalte jeweilige Antwort
      msg = data_from_server.readLine();
      System.out.println("Data from Server : " + msg);
      System.out.println("Timespan: " + (System.nanoTime()-start_send));

      socket.close();
    }catch(IOException ioe){
      ioe.printStackTrace();
    }
  }
}

public static void main(String[] args) {
    try {
        TCPServer tcp = new TCPServer();
        tcp.initialize(6666);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

public void initialize(int port) throws IOException {
    // Erstellen eines Server Sockets
    ServerSocket server = new ServerSocket(port);

    while (true) {
        // Warten bis die Verbindung besteht
        System.out.println("Waiting for client to connect...");
        Socket socket = server.accept();

        // Writer für die ausgehende Antwort
        PrintWriter data_to_client = new PrintWriter(
                socket.getOutputStream(), true);
        // Reader für die ankommende Nachricht
        BufferedReader data_from_client = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));

        // IP-Adresse des Clients
        InetAddress remoteIp = socket.getInetAddress();

        // Erhalten der Nachricht
        String msg = data_from_client.readLine();
        System.out.println("Client " + remoteIp + " : " + msg);

        // Versenden der Antwort
        send(data_to_client, msg);
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top