我坐在一个房间里,笔记本电脑连接到无线网络。 在我旁边,我的朋友正坐在另一台也连接到同一网络的笔记本电脑上。
我们希望使用套接字编程(eclipse / netbeans)连接我们的笔记本电脑。我们应该怎么做?这就是我们到目前为止所做的:
这些是课程。
服务器:
package Connect1;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.net.*;
public class Server1
{
public static void main(String argv[]) throws Exception
{
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
connectionSocket.close();
// welcomeSocket.close();
}
}
}
客户:
package Connect1;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
import java.util.Scanner;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser =new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("IP of my laptop here", 6789);
DataOutputStream outToServer =new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.print("Enter characters to be capitalized: ");
sentence =inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
Scanner input=new Scanner(System.in);
//System.out.print("Do you want to enter again? Press '0' for 'yes' and '1' if 'No'.");
//i=input.nextInt();
}
}
我们收到此错误:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at Code1.TCPClient.main(TCPClient.java:18)
有谁能告诉我我们做错了什么?我们的代码或程序中是否缺少某些内容?我们还需要首先建立其他类型的连接吗?我们是否必须打开其他端口才能执行此操作?可以在两台笔记本电脑上使用不同版本的eclipse来完成吗?
答案 0 :(得分:1)
要创建客户端套接字,您需要使用服务器的IP地址而不是您的。
package Connect1;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
import java.util.Scanner;
class TCPClient {
public static void main(String argv[]) throws Exception
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser =new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(InetAddress.getByName("SERVER_IP"), 6789);
DataOutputStream outToServer =new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer =new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.print("Enter characters to be capitalized: ");
sentence =inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
Scanner input=new Scanner(System.in);
//System.out.print("Do you want to enter again? Press '0' for 'yes' and '1' if 'No'.");
//i=input.nextInt();
}
}
替换&#34; SERVER_IP&#34;通过服务器的IP。
答案 1 :(得分:0)
替换
Socket clientSocket = new Socket("IP of my laptop here", 6789);
带
Socket clientSocket = new Socket(server Address, 6789);
答案 2 :(得分:0)
在命令提示符下,type:ipconfig
这将提供您计算机的IP地址。
现在假设您正在计算机上运行服务器代码,请将从步骤1检索到的IP地址传递给客户端代码(由您的朋友(作为客户端的朋友)运行的代码):
Socket clientSocket = new Socket(<Here goes the IP address of the server machine>, 6789);
现在,无论运行客户机的哪台计算机,都需要传递服务器计算机的IP地址,以便它们可以相互连接。因此,如果您想与您的朋友聊天,请在运行服务器类代码之后,也运行客户端类代码。现在你们两个都已连接到服务器并可以聊天。