gpt4 book ai didi

java - java中的Socket编程问题

转载 作者:行者123 更新时间:2023-12-03 11:52:32 25 4
gpt4 key购买 nike

我不久前从你的网站得到了这个代码。

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

class sevr implements Runnable{
public void run() {
ServerSocket sSkt = null;
Socket skt = null;
BufferedReader br = null;
BufferedWriter bw = null;

try{
System.out.println("Server: is about to create socket");
sSkt = new ServerSocket(6666);
System.out.println("Server: socket created");
}
catch(IOException e){
System.out.println("Server: socket creation failure");
}
try{
System.out.println("Server: is listening");
skt = sSkt.accept();
System.out.println("Server: Connection Established");
}
catch(IOException e){
System.out.println("Server: listening failed");
}
try{
System.out.println("Server: creating streams");
br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
System.out.println("Server: stream done");
}
catch(IOException e){
System.out.println("Server: stream failed");
}
System.out.println("Server: reading the request");
try{
String line = null;
line = br.readLine();
System.out.println("Server: client said-> "+ line);
}
catch(IOException e){
System.out.println("Server: reading failed");
}
System.out.println("Server: reading fished");

System.out.println("Server: responding");
try{
bw.write("Hi! I am server!\n");
bw.flush();
}
catch(IOException e){
System.out.println("Server: responding failed");
}
System.out.println("Server: responding finished");

System.out.println("Server: is finishing");
try {
br.close();
bw.close();
skt.close();
sSkt.close();
} catch (IOException e) {
System.out.println("Server: finishing failed");
}
System.out.println("Server: done");
}
}

class clnt implements Runnable{
public void run() {
Socket skt = null;
BufferedReader br = null;
BufferedWriter bw = null;

try{
System.out.println("Client: about to create socket");
skt = new Socket(InetAddress.getLocalHost(),6666);
System.out.println("Client: socket created");
}
catch(IOException e){
System.out.println("Client: socket creation failure");
}

try{
System.out.println("Client: creating streams");
br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
System.out.println("Client: stream done");
}
catch(IOException e){
System.out.println("Client: stream failed");
}
System.out.println("Client: requesting");
try{
bw.write("Hi! I am Client!\n");
bw.flush();
}
catch(IOException e){
System.out.println("Client: requesting failed");
}
System.out.println("Client: requesting finished");
System.out.println("Client: reading the respond");
try{
String line = null;
line =br.readLine();
System.out.println("Client: server said-> "+ line);
}
catch(IOException e){
System.out.println("Client: reading failed");
}
System.out.println("Client: reading fished");



System.out.println("Client: is finishing");
try {
br.close();
bw.close();
skt.close();
} catch (IOException e) {
System.out.println("Client: finishing failed");
}
System.out.println("Client: done");
}
}


public class Soc {


public static void main(String[] args) {
System.out.println("Main started");
Thread sThread = new Thread(new sevr());
Thread cThread = new Thread(new clnt());
sThread.start();
cThread.start();
try {
sThread.join();
cThread.join();
} catch (InterruptedException ex) {
System.out.println("joining failed");
}
System.out.println("Main done");

}

}

我通过路由器连接到网络。共有 3 台笔记本电脑连接到网络。我在 eclipse 上运行了这段代码。代码成功执行,没有返回任何错误。但是我怎么知道我的笔记本电脑与哪台笔记本电脑建立了连接?我如何确定这一点?

最佳答案

您不会连接到任何其他计算机。该程序在同一台机器上运行客户端和服务器。

skt = new Socket(InetAddress.getLocalHost(),6666);

正如您在此处看到的,客户端通过端口 6666 连接到本地主机。服务器正在监听端口 6666 上的连接。要连接到另一台计算机,您需要将客户端和服务器代码分开,并在不同的机器上运行它们。然后,您必须更改上面的行以将套接字创建为运行服务器的机器的地址。

关于java - java中的Socket编程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5389823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com