Android客户端服务器问题 [英] Android Client Server Issues

查看:72
本文介绍了Android客户端服务器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我真的希望有人可以帮助我。我在大学里正在研究一个android项目,我没有做过android编程,也没有在大学里做任何套接字编程。我在这个项目中的第一步是进行客户端服务器通信。我有基于笔记和教程等我发现在线创建了一个简单的客户端和服务器,当在客户端点击按钮时,字符串被发送到服务器。然后这个字符串打印到控制台...



我已经在下面包含了我的代码,不幸的是,字符串没有打印到控制台,它对我来说是正确的。请问有人可以快速查看一下,看看我可能会出错的地方吗?



我在这个阶段拉我的头发!!





服务器



Hi,

I''m really hoping someone here can help me out. I''m working on an android project in college, I haven''t done android programming, nor have I done any socket programming in college. My first step in this project is to have a client server communication. I have, based on notes and tutorials etc I have found online created a simple client and server, where a string is sent to the server when a button is clicked on the client side. This string is then mean to print to console...

I''ve included my code below, unfortunately, the string doesn''t print to the console, it looks correct to me. Could someone please have a quick look over this and see where I might be going wrong?

I''m pulling my hair out at this stage!!


SERVER

public class Additional_Server {  
  
    private static String message;  
  
    public static void main(String[] args) throws Exception {  
  
          
            Socket s;  
            ServerSocket ss = new ServerSocket(2001);  
              
              
        System.out.println("Server started. Listening to the port 2001");  
  
          
          
        while (true) {  
            try {  
                System.out.println("Server: waiting for connection ..");  
                  
                s = ss.accept();  
                InputStream in = s.getInputStream();  
                Scanner r = new Scanner(in);  
                OutputStream o = s.getOutputStream();  
                PrintWriter p = new PrintWriter(o);  
  
                String inputLine;  
                inputLine = r.nextLine();  
                p.println("Hello " + inputLine + " from Gary");  
                p.close();  
              
              
          
            } catch (IOException ex) {  
                System.out.println("Problem in message reading");  
            }  
        }  
  
    }  
}  





客户







CLIENT


import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.io.PrintWriter;  
import java.net.Socket;  
import java.net.UnknownHostException;  
import java.util.Scanner;  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.TextView;  
  
public class MainActivity extends Activity {  
  
    private Socket s;  
    private PrintWriter p;  
    TextView display;  
    Button test;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        test = (Button) findViewById(R.id.test);  
        display = (TextView) findViewById(R.id.Sdisplay);  
  
        test.setOnClickListener(new View.OnClickListener() {  
  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
  
                try {  
                    s = new Socket("192.168.1.*", 2001); // connect to server  
                    OutputStream o = s.getOutputStream();  
                    PrintWriter p = new PrintWriter(o);  
                    InputStream in = s.getInputStream();  
                    Scanner r = new Scanner(in);  
  
                    p.println("Gary");  
                    p.flush();  
                      
                      
                } catch (UnknownHostException e) {  
                    e.printStackTrace();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
  
    }  
}





提前感谢您



问候,

加里



Thanking you in advance

Regards,
Gary

推荐答案

在您的服务器代码中,您正在编写套接字本身的答案。



您有两种选择:

1.使用

In your Server code, you are writing your answer to the socket itself.

You have two choices:
1. display the message on the server using
System.out.println("Hello " + inputLine + " from Gary");



2.在客户端上阅读答案并将其显示在Activity的小部件中。

您必须检查输入流中的可用数据,读取数据,以及在设置display小部件的文本之前转换为字符串(例如)


2. read the answer on the client and display it in a widget of your Activity.
You have to check for available data in the input stream, read the data, and convert to string before setting the text of the "display" widget (for example)


这篇关于Android客户端服务器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆