Java-Android中的客户端服务器
作者:互联网
我已经尝试了几天来创建一个应用程序客户端服务器,都在android上,并且在同一台计算机上运行.
我使用了各种Display函数来显示客户端和服务器的状态.在奋斗之后,我成功地将它们连接了起来,这并不是很难做到的.
我面临的最大问题是从客户端向服务器发送数据时.
我已经使用了DisplayTitle()函数在服务器端显示从客户端到达那里的数据,但是从不显示任何内容
我认为问题出在发送数据,原因是我正在使用PrintWriter发送数据,并且我读到我应该刷新它才能执行此操作,否则,仅当缓冲区已满时才发送数据. ..我已经做到了,还是一无所有!!!
这是我的客户代码:
public class screen1 extends Activity {
private TextView clientState;
private String serverIpAddress="10.0.2.2";
public static final int ClientPort = 8080;
String message="Hello Server!";
int longitude;
int latitude;
Cursor c;
DBAdapter db;
private Handler handler=new Handler();
Socket socket;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
clientState = (TextView) findViewById(R.id.client_Status);
Button b = (Button)findViewById(R.id.mainMenu);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(screen1.this, screen2.class);
startActivity(i);
}
});
Thread cThread=new Thread(new ClientThread());
cThread.start();
db=new DBAdapter(this);
db.createDatabase();
db.openDataBase();
}
public class ClientThread implements Runnable{
private PrintWriter out=null;
public void run()
{
try
{
InetAddress serverAddr=InetAddress.getByName(serverIpAddress);
handler.post(new Runnable(){
public void run(){
clientState.setText(" try to connect!");
}
});
socket=new Socket(serverAddr,ClientPort);
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
}
catch(UnknownHostException e){
System.err.println("Don't know about host");
}
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to host");
}
if(socket!=null && out!=null){
try{
c=db.getAllData();
if(c.moveToFirst())
{
do{
longitude=Integer.parseInt(c.getString(1));
out.println(longitude);
out.flush();
latitude=Integer.parseInt(c.getString(2));
out.println(latitude);
out.flush();
}while(c.moveToNext());
}
out.close();
socket.close();
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
}
}
这是我的服务器,在DisplayTitle()中,我尝试显示应该从客户端接收的纬度和经度:
public class Server1 extends Activity {
//the ip the server is listening to
public static String SERVERIP="10.0.2.15";
Socket client;
Scanner s=null;
TextView serverStatus;
Handler handler=new Handler();
protected int ServerPort= 6000;
int longitude;
int latitude;
protected boolean isStopped = false;
private ServerSocket serverSocket=null;
private Socket clientSocket=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server1);
Button b = (Button)findViewById(R.id.mainMenu);
serverStatus =(TextView)findViewById(R.id.server_Status);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0){Intent i = new Intent(Server1.this, Server2.class);
startActivity(i);}});
Thread fst=new Thread(new ServerThread());
fst.start();
// MultiThreadedServer server = new MultiThreadedServer(6000);
// new Thread(server).start();
}
public class ServerThread implements Runnable{
public void run(){
try{
InetSocketAddress serverAddr =new InetSocketAddress(SERVERIP,6000);
serverSocket = new ServerSocket();
serverSocket.bind(serverAddr);
handler.post(new Runnable(){public void run() {
serverStatus.setText("wait for clients");
}});
while(true){
//accept for incoming clients
client= serverSocket.accept();
handler.post(new Runnable(){public void run() {
serverStatus.setText("Connected.");
}});
try{
s=new Scanner(new BufferedReader(new InputStreamReader(client.getInputStream())));
while(s.hasNext()){
longitude=Integer.parseInt(s.next());
System.out.println(longitude);
latitude=Integer.parseInt(s.next());
System.out.println(latitude);
// db.insertData2(longitude,latitude);
handler.post(new Runnable() {
public void run() {
DisplayTitle(longitude,latitude);
}
});
}
break;
}
catch(Exception e)
{
handler.post(new Runnable(){
public void run(){
serverStatus.setText("Connection interrupted!!");
}
});
e.printStackTrace();
}
}
}
catch(Exception e)
{
handler.post(new Runnable()
{
public void run(){
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}
public void DisplayTitle(int longitude,int latitude)
{
Toast.makeText(this,
"longitude: " + longitude +"\n" +
"latitude: " + latitude +"\n" ,
Toast.LENGTH_LONG).show();
}
}
如果有人对我做错了任何想法,请告诉我!我在这里了解更多详情.
解决方法:
如果您只想查看在客户端或服务器中发送的数据,那么使用调试输出显示您正在接收数据有什么问题?您可以为此使用logcat.当您接收或发送数据时,只需将数据打印到logcat.
Log.d(string, string);
标签:client-server,java,android 来源: https://codeday.me/bug/20191102/1993557.html