[更新/回答]
我试图在Windows上使用NetBeans在android客户端应用程序和GUI java程序之间建立连接。
服务器启动并正常工作但是当我尝试使用相同的端口号将客户端连接到它时,它失败了,这里是代码:
Windows上的服务器:
package server.test;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JOptionPane;
public class ServerThread extends Thread{
Server serverform;
ObjectInputStream OIS;
ObjectOutputStream OUS;
ServerSocket serverSocket;
Socket socket;
public ServerThread(Server serverForm){
this.serverform = serverform;
try{
serverSocket = new ServerSocket(1789);
start();
JOptionPane.showMessageDialog(serverform, "Server started!");
}catch(Exception e){
e.printStackTrace();
}
}
public void sendMessage(String msg){
try{
OUS.writeObject(msg.toString());
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void run(){
while(true){
try{
socket = serverSocket.accept();
openReader();
}catch(Exception e){
e.printStackTrace();
}
}
}
public void openReader(){
try{
OIS = new ObjectInputStream(socket.getInputStream());
OUS = new ObjectOutputStream(socket.getOutputStream());
MsgRecThread mrt = new MsgRecThread(serverform, OIS, OUS);
}catch(Exception e){
e.printStackTrace();
}
}
public class MsgRecThread extends Thread{
Server serverform;
ObjectInputStream OIS;
ObjectOutputStream OUS;
public MsgRecThread(Server serverForm, ObjectInputStream OIS, ObjectOutputStream OUS){
this.serverform = serverform;
this.OIS = OIS;
this.OUS = OUS;
start();
}
public void run(){
while(true){
try{
serverform.jTextArea1.append(OIS.readObject().toString() + "\n");
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
这是Android Studio上的客户端代码:
MainActivity.java
01 : package com.mousa.alfhaily.client_test3;
02 :
03 : import android.support.v7.app.AppCompatActivity;
04 : import android.os.Bundle;
05 : import android.view.View;
06 : import android.widget.Button;
07 : import android.widget.TextView;
08 : import java.io.ObjectInputStream;
09 : import java.io.ObjectOutputStream;
10 : import java.net.Socket;
11 :
12 : public class MainActivity extends AppCompatActivity {
13 : Socket socket;
14 :
15 : ObjectInputStream OIS;
16 : ObjectOutputStream OUS;
17 :
18 : @Override
19 : protected void onCreate(Bundle savedInstanceState) {
20 : super.onCreate(savedInstanceState);
21 : setContentView(R.layout.activity_main);
22 :
23 : Button sendBUT = (Button) findViewById(R.id.sendBUT);
24 : Button stcBUT = (Button) findViewById(R.id.stcBUT);
25 :
26 : stcBUT.setOnClickListener(
27 : new Button.OnClickListener(){
28 : public void onClick(View v){
29 : Button stcBUT = (Button) findViewById(R.id.stcBUT);
30 : try{
31 : socket = new Socket("127.0.0.1", 1789);
operReader();
stcBUT.setText("Client connected!");
stcBUT.setEnabled(false);
}catch(Exception e){
stcBUT.setText("Try again!");
e.printStackTrace();
}
}
}
);
sendBUT.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
try{
OUS.writeObject(R.id.editText);;
}catch(Exception e){
e.printStackTrace();
}
}
}
);
}
private void operReader() {
try{
OUS = new ObjectOutputStream(socket.getOutputStream());
OIS = new ObjectInputStream(socket.getInputStream());
MsgRecThread mrt = new MsgRecThread(this, OUS, OIS);
}catch(Exception e){
e.printStackTrace();
}
}
private class MsgRecThread extends Thread{
MainActivity ma;
ObjectOutputStream OUS;
ObjectInputStream OIS;
public MsgRecThread(MainActivity ma, ObjectOutputStream OUS, ObjectInputStream OIS){
this.ma = ma;
this.OIS = OIS;
this.OUS = OUS;
start();
}
public void run(){
while(true){
try{
TextView receivedMessages = (TextView)findViewById(R.id.receivedMessages);
receivedMessages.append(OIS.readObject().toString() + "\n");
}catch(Exception e){
e.printStackTrace();
}
}
}
}
}
错误发生在行中:
socket = new Socket("127.0.0.1", 1789);
当我尝试连接客户端时,这是logcat:
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at libcore.io.IoBridge.socket(IoBridge.java:576)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at java.net.Socket.startupSocket(Socket.java:560)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at java.net.Socket.tryAllAddresses(Socket.java:128)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at java.net.Socket.<init>(Socket.java:178)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at java.net.Socket.<init>(Socket.java:150)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at com.mousa.alfhaily.client_test3.MainActivity$1.onClick(MainActivity.java:31)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at android.view.View.performClick(View.java:4496)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at android.view.View$PerformClick.run(View.java:18603)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at android.os.Looper.loop(Looper.java:136)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5433)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at dalvik.system.NativeStart.main(Native Method)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at libcore.io.Posix.socket(Native Method)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: at libcore.io.IoBridge.socket(IoBridge.java:561)
08-28 16:38:23.343 16841-16841/com.mousa.alfhaily.client_test3 W/System.err: ... 17 more
更新/答案:
我无法使用IP(127.0.0.1),因为客户端和服务器不在同一台机器上,我必须使用服务器的IP,这是我的PC本地IP地址。