user3440629
user3440629

Reputation: 198

Unable to start socket server java

I am trying to start a socket server by calling the class's default constructor, however server is not starting

Below is my server socket class:

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


public class TransactionServer
{
    public void TransactionServer() throws IOException {

        System.out.println("Inside Create Server");
        Socket echoSocket;
        InputStream sin = null;
        OutputStream sout  = null;

        ObjectInputStream sinO=null;
        ObjectOutputStream soutO=null;

        try {
           ServerSocket s = new ServerSocket(2000);
           System.out.println("Server Ruinning");
           echoSocket = s.accept();
           System.out.println("Connection from: " + echoSocket.getInetAddress());

           sinO = new ObjectInputStream(echoSocket.getInputStream());
           soutO = new ObjectOutputStream(echoSocket.getOutputStream());

           String temp = (String) sinO.readObject();
           System.out.println("" + temp);
        } catch (Exception e) {
            System.err.println(e);
            return;
        }
    }
}

Below is my invocation of object of this class:

public class TabbedPane extends JFrame {

    public TabbedPane() {

        TransactionServer newServer=new TransactionServer();
    }
}

However the server is not starting.

Upvotes: 0

Views: 324

Answers (2)

Manish Maheshwari
Manish Maheshwari

Reputation: 4134

You have used void when defining the TransactionServer constructor. Remove the void, and the constructor will be triggered, to run the socket server.

Replace line:

public void TransactionServer() throws IOException {

With:

public TransactionServer() throws IOException {

Other than this, I am assuming you have a main method somewhere to invoke the "invokation object". :-)

Upvotes: 1

Fevly Pallar
Fevly Pallar

Reputation: 3099

Your newServer instance doesn't call TransactionServer() ! so :

TransactionServer newServer=new TransactionServer();
                  newServer.TransactionServer();

Upvotes: 1

Related Questions