Java客户端服务器套接字相关问题

huangapple go评论78阅读模式
英文:

Java Client Server Socket related problem

问题

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

public class Client {

    public static void main(String[] args) throws Exception {
        
        String ip = "localhost";
        int port = 5643;
        Socket s = new Socket(ip, port);
        String str = "Sagar Tanwar";
        OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
        PrintWriter pw = new PrintWriter(os);
        os.write(str);
        os.flush();
    }
}
import java.net.*;
import java.io.*;

public class Server {

    public static void main(String[] args) throws Exception {
        
        System.out.println("Server is started");
        ServerSocket ss = new ServerSocket(5643);
        
        System.out.println("Server is waiting");
        Socket s = ss.accept();
        
        System.out.println("Client Connected");
        
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String str = br.readLine();
        
        System.out.println("Client Data: " + str);
    }
}
Exception in thread "main" 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:606)
        at java.net.Socket.connect(Socket.java:555)
        at java.net.Socket.<init>(Socket.java:451)
        at java.net.Socket.<init>(Socket.java:228)
英文:
import java.io.*;
import java.net.*;

public class Client {

	public static void main(String[] args) throws Exception {
		
		String ip = &quot;localhost&quot;;
		int port = 5643;
		Socket s = new Socket(ip, port);
		String str = &quot;Sagar Tanwar&quot;;
		OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
		PrintWriter pw = new PrintWriter(os);
		os.write(str);
		os.flush();
	}
}
import java.net.*;
import java.io.*;

public class Server {

	public static void main(String[] args) throws Exception {
		
		System.out.println(&quot;Server is started&quot;);
		ServerSocket ss = new ServerSocket(5643);
		
		System.out.println(&quot;Server is waiting&quot;);
		Socket s = ss.accept();
		
		System.out.println(&quot;Client Connected&quot;);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		String str = br.readLine();
		
		System.out.println(&quot;Client Data : &quot;+str);
	}
}
Exception in thread &quot;main&quot; 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:606)
        at java.net.Socket.connect(Socket.java:555)
        at java.net.Socket.&lt;init&gt;(Socket.java:451)
        at java.net.Socket.&lt;init&gt;(Socket.java:228)

答案1

得分: 3

Client.java 中将这一行更改为:

String str = "Sagar Tanwar\r\n";

您的服务器希望读取一整行(br.readLine()),并会持续读取直到客户端保持连接。一旦客户端断开连接,就会抛出异常,因为服务器无法读取新的一行。

附注

在我的系统上:

  • 如果服务器未启动,您的客户端会抛出:java.net.ConnectException: Connection refused: connect
  • 如果服务器无法从客户端读取完整的一行,您的服务器会抛出:java.net.SocketException: Connection reset
英文:

In Client.java change this line:

String str = &quot;Sagar Tanwar&quot;;

to:

String str = &quot;Sagar Tanwar\r\n&quot;;

Your server expects to read a full line (br.readLine()) and it continues to read until your client stays connected. Once the client disconnects, an exception is thrown because the server was unable to read a new line.

Side notes

On my system:

  • your client throws: java.net.ConnectException: Connection refused: connect - if the server is not active
  • your server throws: java.net.SocketException: Connection reset - if the server can't read a full line from the client

答案2

得分: 0

一切都会为您正常工作,如果您在客户端类的末尾关闭连接。

String ip = "localhost";
int port = 5643;
Socket s = new Socket(ip, port);
String str = "Sagar Tanwar";
OutputStreamWriter os = null;
try {
    os = new OutputStreamWriter(s.getOutputStream());
    os.write(str);
    os.flush();
} finally {
    if (os != null)
        os.close();
}

或者使用“try with resources”:

String ip = "localhost";
int port = 5643;
Socket s = new Socket(ip, port);
String str = "Sagar Tanwar";
try (OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream())) {
    os.write(str);
    os.flush();
}
英文:

Everything will work for you if you close the connections at the end of the client class.

    String ip = &quot;localhost&quot;;
    int port = 5643;
    Socket s = new Socket(ip, port);
    String str = &quot;Sagar Tanwar&quot;;
    OutputStreamWriter os = null;
    try {
        os = new OutputStreamWriter(s.getOutputStream());
        os.write(str);
        os.flush();
    } finally {
        if (os != null)
            os.close();
    }

or with "try with resources"

    String ip = &quot;localhost&quot;;
    int port = 5643;
    Socket s = new Socket(ip, port);
    String str = &quot;Sagar Tanwar&quot;;
    try (OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream())) {
        os.write(str);
        os.flush();
    }

答案3

得分: -2

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws Exception {

        String ip = "localhost";
        int port = 5643;
        Socket s = new Socket(ip, port);
        String str = "Sagar Tanwar";
        OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
        PrintWriter pw = new PrintWriter(os);
        os.write(str);
        os.flush();
    }
}
英文:
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws Exception {

        String ip = &quot;localhost&quot;;
        int port = 5643;
        Socket s = new Socket(ip, port);
        String str = &quot;Sagar Tanwar&quot;;
        OutputStreamWriter os = new OutputStreamWriter(s.getOutputStream());
        PrintWriter pw = new PrintWriter(os);
        os.write(str);
        os.flush();
    }
}

huangapple
  • 本文由 发表于 2020年9月25日 02:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64052438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定