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

热点内容
程序员想要的小礼物 浏览:184
java获取网页url 浏览:624
怎么做解压神器泡泡版 浏览:966
自己动手做一个c编译器 浏览:929
手机如何链接谷歌服务器地址 浏览:137
废掉一个程序员的武功 浏览:249
java树形算法 浏览:641
通达信加锁指标源码怎么看 浏览:754
将同名文件移动到部分同名文件夹 浏览:403
摆荡指标加压力线源码 浏览:915
新一代单片机特征 浏览:770
王者的服务器什么时候才修好 浏览:281
cad历史命令 浏览:41
php博客源代码 浏览:24
cng压缩机功率 浏览:459
pdf批量打印怎么设置 浏览:78
javamap底层 浏览:267
卖华为云服务器 浏览:111
中颖单片机烧录器 浏览:848
pdf转换成图片免费软件 浏览:469