導航:首頁 > 配伺服器 > socket如何獲取伺服器數據

socket如何獲取伺服器數據

發布時間:2023-10-19 18:43:52

android socket 接收伺服器返回數據,如何查找想要的數值

Android 客戶端代碼:

package com.ltb.SCActivity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SCActivity extends Activity {
private TextView mRecvText = null;
private EditText mSentText = null;
private Button mSendRecvButton = null;
private Socket mSocket = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mRecvText = (TextView)findViewById(R.id.recvText);
mSentText = (EditText)findViewById(R.id.sentText);
mSendRecvButton = (Button)findViewById(R.id.sendRecvButton);

mSendRecvButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String sentMessage = mSentText.getText().toString() + "\r\n";

try {
mSocket = new Socket("10.126.38.155", 12577);

Log.i("------SCActivity---before send---", sentMessage);

PrintWriter send = new PrintWriter(new BufferedWriter(new OutputStreamWriter(mSocket.getOutputStream())));
send.println(sentMessage);
send.flush(); //Important!!!!!!
//send.write(sentMessage);
Log.i("------SCActivity---after send---", sentMessage);

BufferedReader recv = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
Log.i("------SCActivity---Before ---recv.readLine()---", sentMessage);
String recvMsg = recv.readLine();
Log.i("------SCActivity---after ---recv.readLine()---", recvMsg);
if (recvMsg != null) {
mRecvText.setText(recvMsg);
} else {
mRecvText.setText("Cannot receive data correctly.");
}

send.close();
recv.close();
mSocket.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}

伺服器端代碼:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server implements Runnable {
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(12577);
while (true) {
Socket client = serverSocket.accept();
System.out.println("accept");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
System.out.println("read:" + str);
PrintWriter pout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())), true);
//pout.write("Server Received Message: " + str);
pout.println("Server Received Message: " + str);

System.out.println("after send in server");
/* BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
System.out.println("read:" + str);*/

pout.close();
in.close();
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
client.close();
System.out.println("close");
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

public static void main(String argv[] ) {
Thread pcServerThread = new Thread(new Server());
pcServerThread.start();
}
}

㈡ Java中伺服器端ServerSocket對象怎麼獲取伺服器端地址和埠號,怎麼獲取遠程請求的

ServerSocket s = new ServerSocket(8888);
while (true) {
// 建立連接
Socket socket = s.accept();

/ /getInetAddress()獲取遠程ip地址,getPort()遠程客戶端的斷後好
"你好,客戶端地址信息: " + socket.getInetAddress() + "\t客戶端通信埠號: " + socket.getPort()

閱讀全文

與socket如何獲取伺服器數據相關的資料

熱點內容
max加線命令 瀏覽:424
app胖瘦模式哪個好用 瀏覽:724
可以下載源碼的軟體 瀏覽:487
程序員寫一天代碼累嗎 瀏覽:628
ie文件夾禁止訪問 瀏覽:543
百川互聯網程序員 瀏覽:783
linuxpython解釋器 瀏覽:667
興安得力軟體加密狗 瀏覽:492
智能網路攝像頭加密 瀏覽:574
軟體畢業程序員培訓 瀏覽:652
安卓陀螺儀低怎麼辦 瀏覽:247
一級建造師復習題集pdf 瀏覽:903
法理學pdf海默 瀏覽:392
伺服器內存儲器是用什麼的 瀏覽:819
微幫同城分類信息源碼 瀏覽:808
安卓系統ad是什麼 瀏覽:473
python輸出中不加佔位符 瀏覽:597
linux文件夾許可權控制 瀏覽:732
雅虎郵箱怎麼加密碼 瀏覽:823
為什麼安卓手機登錄不了蘋果賬號 瀏覽:537