English 中文(简体)
开放联系
原标题:Android Java Socket Socket TCPIP open connection

我需要通过国际防止母婴传播方案与服务器连接。 我发现,如何做到这一点,但例子没有形成永久联系,只是把数据发送,并缩小了联系。 我需要离开这一联系,以收回和提出几个包裹。

例如,我用来要求联系,然后开展一个过程,听取新数据。 与此类似:

  1. Connect
  2. Acknowledge connection
  3. Send data
  4. Receive data ... go back to #3
  5. Close the connection.

我是否可以读到这部或比较好的样本法?

最佳回答

我以不同用途提出了类似的申请,只有服务器方面没有听力,只有发送。 然而,它确实在 lo中敞开大门,在接到具体信息后,你可以加以修改,在 lo期间使用强硬的指挥手段关闭通道和连接。 见这个专题:。 用于编码的WiFi TCP链接中的垃圾数据,从桌面到,并改行:

n = write(newsockfd, string, sizeof(string));

纽约总部

n = write(newsockfd, string, strlen(string));

in order 纽约总部fix the issue I was having.

问题回答

从现在起(ICS+),为了建立网络联系,除非你愿意取消限制,否则你将需要使用AyncTask(或手递)。 网络连接因缺省而受主要活动的限制。

页: 1 任务确实是trick(在另一个方面,即拥有一台电脑服务器)。 在此情况下,一开始计票,发出指示,表明一是从主要活动收到的,收到答复,发出另一信息(当时是“在那里”),并在关闭联系之前收到最后信息。

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.AsyncTask;
import android.util.Log;

public class SocketConnect extends AsyncTask <String, Integer, String> {

    String TAG = "TGsocket";

    @Override
    protected String doInBackground(String... params) {
        String url = params[0];
        String textSend = params[1];
        Log.i(TAG, "textsend = |" + textSend + "|");
        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;
        try {
            socket = new Socket(url, 9090);
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());
            Log.i(TAG,"socket OK");
            dataOutputStream.writeUTF(textSend);
            String textInNow = dataInputStream.readLine();
            Log.w(TAG, "says Server = " + textInNow);
            dataOutputStream.writeUTF("hello there");
            textInNow = dataInputStream.readLine();
            Log.w(TAG, "says 2ndTime Server = " + textInNow);
        } catch (UnknownHostException e) {
            Log.e(TAG, "at thread unknownHost " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "at thread IO " + e.getMessage());
            e.printStackTrace();
        }

        finally{
            Log.i(TAG, "finally");
            if (dataOutputStream != null){
                try {
                    dataOutputStream.close();
                } catch (IOException e) {
                    Log.e(TAG, "at thread dataoutput IO " + e.getMessage());
                    e.printStackTrace();
                }
            }

            if (dataInputStream != null){
                try {
                    dataInputStream.close();
                } catch (IOException e) {
                    Log.e(TAG, "at thread datainput IO " + e.getMessage());
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    Log.i(TAG, "socket closed");
                    socket.close();
                } catch (IOException e) {
                    Log.e(TAG, "at thread finally IO " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
       @Override
       protected void onPreExecute() {
          super.onPreExecute();
          //displayProgressBar("Downloading...");
       }
    @Override
       protected void onProgressUpdate(Integer... values) {
          super.onProgressUpdate(values);
          //updateProgressBar(values[0]);
       }

       @Override
       protected void onPostExecute(String result) {
          super.onPostExecute(result);
       }
}

使用电话(在试捕中):

SocketConnect("100.100.100.100","some string");

“100.100.100”是你的服务器IP。

NOTE1: do not forget to set permissions for internet use (in your manifest file) NOTE2: you can safely strip away the Log calls and most of the checks, as well as the 3 final overrides (i just left it all there because it makes it easier, in some cases, to build upon).

在froyo、gingerbread、ics和jb试验。

你们是否应该选择像“Apache MINA”或“Netty”等更高级别的图书馆?





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签