简体   繁体   中英

Java socket programming, 2-way communication

I'm trying to create a two-way communication between the server and client, where both parties (server and client) can send messages to each other without having to wait for the other one to respond. For that I've created a separate thread to listen on the socket and print the incoming messages on the screen. Here's the real problem: The first time I compile and run server and client everything works fine when I run the programs afterwards I get this error message from both server and client.

java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at Build.work(c.java:18)
at s.main(s.java:83)

Exception in thread "Thread-0" Exception in thread "main" java.lang.NullPointerException
at Build.run(c.java:59)
at java.lang.Thread.run(Thread.java:748)

java.lang.NullPointerException
at Build.speak(c.java:35)
at c.main(c.java:85)

server

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

class Build implements Runnable
{
    int  port = 3500;
    String input1;
    ServerSocket server;
    Socket socket;
    DataInputStream in;
    DataOutputStream out;
    Console reader;

    public void work()
    {
        try
        {
            server = new ServerSocket(port);
            socket = server.accept();
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            reader = System.console();
        }

        catch(IOException e){
        e.printStackTrace();}
    }

    public void speak()
    {
        try
        {
            while(true)
            {
                input1 = reader.readLine();
                out.writeUTF(input1);
                if(input1.equals("stop"))
                {
                    in.close();
                    out.close();
                    socket.close();
                    server.close();
                    System.exit(0);
                }               
            }
        }
        catch (IOException e){
        e.printStackTrace();}
    }

    public void run()
    {
        String input2;

        try
        {
            while(true)
            {
                input2 = in.readUTF();
                System.out.println(input2);
                if(input2.equals("stop"))
                {
                    in.close();
                    out.close();
                    socket.close();
                    server.close();
                    System.exit(0);
                }
             }
         }
        catch(IOException e){
        e.printStackTrace();}
     }
}

public class server
{
    public static void main(String[]args)
    {
        Build object = new Build();
        object.work();
        Thread t1 = new Thread(object);
        t1.start();
        object.speak();
    }
}

client

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

class Build implements Runnable
{
    String ip = "127.0.0.1";
    int  port = 3500;
    String input1;
    Socket socket;
    DataInputStream in;
    DataOutputStream out;
    Console reader;

    public void work()
    {
        try
        {
            socket = new Socket(ip, port);
            in = new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            reader = System.console();
        }

        catch(IOException e){
        e.printStackTrace();}
    }

    public void speak()
    {
        try
        {
            while(true)
            { 
                input1 = reader.readLine();
                out.writeUTF(input1);
                if(input1.equals("stop"))
                {
                    in.close();
                    out.close();
                    socket.close();
                    System.exit(0);
                 }              
             }
         }
         catch (IOException e){
         e.printStackTrace();}
     }

     public void run()
     {
         String input2;

         try
         {
             while(true)
             {
                 input2 = in.readUTF();
                 System.out.println(input2);
                 if(input2.equals("stop"))
                 {
                     in.close();
                     out.close();
                     socket.close();
                     System.exit(0);
                 }
              }
         }
         catch(IOException e){
         e.printStackTrace();}
     }

}

public class client
{
    public static void main(String[]args)
    {
        Build object = new Build();
        object.work();
        Thread t1 = new Thread(object);
        t1.start();
        object.speak();
    }
}

There is no server here. A server would create a ServerSocket and then start calling accept() . A client would connect to the ServerSocket opened before.

All you have here is two clients.

You may also be confused between readUTF() and readLine() . readUTF() can only read data written by writeUTF() , and readLine() cannot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM