我有一个 tcp/ip 应用程序,客户端“A”在其中获取它的坐标并将它们发送到我的电脑上运行的服务器。我正在使用本地 ips 等。这运行良好,但是我今天早上出来了,我的 tp-link 路由器和局域网有问题,所以我重新安装了我的路由器并且所有连接(wi-fi)都备份了和运行。但是,现在当我尝试运行我的应用程序时,它不适用于我的设备。
如果我通过模拟器运行它(我只有一个用于测试的字符串)它可以工作,我知道这是因为服务器和模拟器都在同一台机器上。我的 ip 是正确的,它是运行服务器的机器的 ip……我正在尝试自学这项技术并为大学做一个项目,但我一直遇到这样的巨大头痛。我已经在下面发布了我的客户端和服务器代码,有没有人可能有任何想法?路由器上的所有设置都相同,我确定这只是通过局域网连接,我不必转发任何端口?
客户A
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class Child extends Activity implements LocationListener {
private Socket s;
private PrintWriter p;
public static double latitude;
public static double longitude;
String coordinates;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.child);
LocationManager mlocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocationListener = new MyLocationListener();
mlocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocationListener);
String hello = "hello"; //FOR TESTING PURPOSES
Transmit (hello);
Log.d("test", "test");
}
public class MyLocationListener implements LocationListener {
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Disabled",
Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "Gps Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longitude = location.getLongitude();
coordinates = ("TESTING " + latitude + longitude);
//Transmit(coordinates);
}
}
private void Transmit(final String message) {
Thread trans = new Thread(new Runnable() {
public void run() {
Log.d("TRANSMIT", "CALLED");
// TODO Auto-generated method stub
try {
s = new Socket("192.168.3.103", 1111); // connect to
// server
Log.d("CONNECTED", "Connected");
DataOutputStream _OutPut = new DataOutputStream(
s.getOutputStream());
_OutPut.writeBytes(message + "\n");
_OutPut.flush();
_OutPut.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
trans.start();
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
服务器
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class TCPServer {
public static void main(String[] args) throws Exception {
Socket s;
ServerSocket ss = new ServerSocket(1111);
System.out.println("Server started. Listening to the port 2001");
System.out.println("Server: waiting for connection ..");
while (true) {
try {
s = ss.accept();
if (s != null) {
InputStream fromChild = s.getInputStream();
while (s.isConnected()) {
System.out.println("Child Connected");
Scanner r = new Scanner(fromChild);
String location;
location = r.nextLine();
System.out.println(location);
}
}
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
所以请,如果有人可以帮助或阐明情况,我将非常感激,因为在我解决这个问题之前我无法进一步发展。
问候,加里