英文:
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 = "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)
答案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 = "Sagar Tanwar";
to:
String str = "Sagar Tanwar\r\n";
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 = "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();
}
or with "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();
}
答案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 = "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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论