Java 实现客户端服务器模型(使用 TCP)并从服务器发送 IP 地址并在客户端打印出来
Java Implementing a Client Server Model (Using TCP) and sending a IP address from server and Printing it out on Client
服务器代码:
package exp1;
import java.net.*;
import java.io.*;
public class MyServerSocket{
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket ss=new ServerSocket(2050);
System.out.println("server is waiting...");
Socket s=ss.accept();
InetAddress ad= InetAddress.getByName("hostname");
OutputStream os=s.getOutputStream();
System.out.println("s"+ ad.getHostAddress());
byte ins=Byte.parseByte(ad.getHostAddress());
os.write(ins);
ss.close();
}
}
客户代码:
package exp1;
import java.io.*;
import java.net.*;
public class MyClientSocket {
public static void main(String[] args) throws Exception{
Socket s=new Socket(InetAddress.getLocalHost(),2050);
InputStream is=s.getInputStream();
System.out.println("Client is ready to receive data");
int d=0;
while(d!='#')
{
d=is.read();
System.out.print((char)d);
}
}
}
错误:
服务器端:
Exception in thread "main" java.lang.NumberFormatException: For input
string: "ip"
at
java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at
java.base/java.lang.Integer.parseInt(Integer.java:660)
at
java.base/java.lang.Byte.parseByte(Byte.java:193)
at
java.base/java.lang.Byte.parseByte(Byte.java:219)
at
exp1/exp1.MyServerSocket.main([MyServerSocket.java:14](https://MyServerSocket.java:1
我试图在客户端上显示本地主机的 ip,但出现错误。
getHostAddress
returns 地址的字符串表示形式。因此,此方法 returns 类似于 192.168.1.100
,无法解析为字节。您可以将字符串作为字节数组传递,但这不是最佳解决方案,因为 IPv4 地址只有 4 个字节,但字符串 192.168.1.100
的长度为 13 个字节!
此外,我不明白第 while (d != '#')
行的用途,因为您从不发送任何 #
符号。
这是适合我的代码
class MyServerSocket {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(2050);
System.out.println("server is waiting...");
Socket s = ss.accept();
InetAddress ad= InetAddress.getByName("hostname");
try (OutputStream os = s.getOutputStream()) {
System.out.println("s"+ ad.getHostAddress());
os.write(ad.getAddress());
}
ss.close();
}
}
class MyClientSocket {
public static void main(String[] args) throws Exception{
Socket s=new Socket(InetAddress.getLocalHost(),2050);
try (InputStream is = s.getInputStream()) {
System.out.println("Client is ready to receive data");
byte[] address = is.readAllBytes();
InetAddress ad = InetAddress.getByAddress(address);
System.out.println(ad.getHostAddress());
}
}
}
P.S。关闭您打开的所有资源是避免泄漏的好习惯。在示例中,我使用了 try-with-resources 构造。
服务器代码:
package exp1;
import java.net.*;
import java.io.*;
public class MyServerSocket{
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket ss=new ServerSocket(2050);
System.out.println("server is waiting...");
Socket s=ss.accept();
InetAddress ad= InetAddress.getByName("hostname");
OutputStream os=s.getOutputStream();
System.out.println("s"+ ad.getHostAddress());
byte ins=Byte.parseByte(ad.getHostAddress());
os.write(ins);
ss.close();
}
}
客户代码:
package exp1;
import java.io.*;
import java.net.*;
public class MyClientSocket {
public static void main(String[] args) throws Exception{
Socket s=new Socket(InetAddress.getLocalHost(),2050);
InputStream is=s.getInputStream();
System.out.println("Client is ready to receive data");
int d=0;
while(d!='#')
{
d=is.read();
System.out.print((char)d);
}
}
}
错误:
服务器端:
Exception in thread "main" java.lang.NumberFormatException: For input string: "ip"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:660)
at java.base/java.lang.Byte.parseByte(Byte.java:193)
at java.base/java.lang.Byte.parseByte(Byte.java:219)
at exp1/exp1.MyServerSocket.main([MyServerSocket.java:14](https://MyServerSocket.java:1
我试图在客户端上显示本地主机的 ip,但出现错误。
getHostAddress
returns 地址的字符串表示形式。因此,此方法 returns 类似于 192.168.1.100
,无法解析为字节。您可以将字符串作为字节数组传递,但这不是最佳解决方案,因为 IPv4 地址只有 4 个字节,但字符串 192.168.1.100
的长度为 13 个字节!
此外,我不明白第 while (d != '#')
行的用途,因为您从不发送任何 #
符号。
这是适合我的代码
class MyServerSocket {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(2050);
System.out.println("server is waiting...");
Socket s = ss.accept();
InetAddress ad= InetAddress.getByName("hostname");
try (OutputStream os = s.getOutputStream()) {
System.out.println("s"+ ad.getHostAddress());
os.write(ad.getAddress());
}
ss.close();
}
}
class MyClientSocket {
public static void main(String[] args) throws Exception{
Socket s=new Socket(InetAddress.getLocalHost(),2050);
try (InputStream is = s.getInputStream()) {
System.out.println("Client is ready to receive data");
byte[] address = is.readAllBytes();
InetAddress ad = InetAddress.getByAddress(address);
System.out.println(ad.getHostAddress());
}
}
}
P.S。关闭您打开的所有资源是避免泄漏的好习惯。在示例中,我使用了 try-with-resources 构造。