导航:首页 > 配服务器 > 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如何获取服务器数据相关的资料

热点内容
全新胜达天窗能编程吗 浏览:320
ug常用命令 浏览:349
为什么碳数增加密度减小 浏览:420
少儿计算机编程培训无聊吗 浏览:589
安卓界面更新时点击卡顿如何解决 浏览:773
日本十大漫画app哪个好用 浏览:878
做系统选择哪个文件夹 浏览:285
如何登陆mc服务器 浏览:801
华为无法定位服务器地址 浏览:963
编译原理第三版陈火旺课本图片 浏览:566
cad用什么解压缩软件 浏览:717
编译的函数模版 浏览:361
加密货币利率改变 浏览:228
复杂网络案例python 浏览:298
死命令的意思 浏览:691
哪个app可以听日语电台 浏览:105
谷轮压缩机15hp 浏览:291
python任意整数冒泡降序 浏览:32
医保卡的钱哪个app能看到 浏览:578
主服务器崩溃如何进行域迁移 浏览:319