TCP Socket 通信 小例子:
1 |
前提:电脑端当做服务器,Android手机作为客户端,在同一个局域网内
|
流程:
1.电脑端服务器打开,监听端口号:9998
2.当手机连上服务器,发送 "您好,服务器!"的消息,服务器通过DataInputStream接收之后,打印出来
3.服务器接收并打印消息之后,随机发送“您好,客户端!”的消息给手机客户端
手机刚打开客户端的界面:
手机点击"连接服务器"之后,服务器收到消息:
服务器返回给客户端的消息:
电脑服务器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package TestSocket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import javax.net.ssl.SSLContext;
public class TcpServer {
/**
* 电脑服务器端,监听端口号9998,接收消息之后,再回复消息
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
while (true) {
ServerSocket ss = new ServerSocket(9998);// 监听9998端口
Socket socket = ss.accept();
//等待客户端连上,并等待接收数据
DataInputStream dis = new DataInputStream(socket.getInputStream());
System.out.println(dis.readUTF()); //打印出客户端发来的数据
//回复消息给客户端
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("您好,客户端!");
ss.close();//通信完之后要关闭,不然下次会报错
dos.close();
dis.close();
}
}
}
|
Android手机客户端程序
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
< RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
xmlns:tools ="http://schemas.android.com/tools"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent" >
<TextView
android:id ="@+id/tv"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_centerHorizontal ="true"
android:layout_centerVertical ="true"
android:text ="@string/hello_world"
/>
<Button
android:id ="@+id/btnconnect"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text = "连接服务器" />
</ RelativeLayout>
|
MainActivity.java 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
package com.example.socketclient;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
public class MainActivity extends Activity {
TextView tv;// 用来显示服务器返回的消息 :“您好,客户端!”
Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btnconnect);// 连接服务器的按钮
btn.setOnClickListener(listener);
tv = (TextView) findViewById(R.id.tv);
mHandler = new MessageHandler();
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
new ReceiveMessageThread().start();// 开启客户端线程
}
};
/**
* 异步操作,TextView显示服务器发来的消息
*
* @author S.Rain
*
*/
class MessageHandler extends Handler {
@Override
public void handleMessage(Message msg) {
tv.setText(msg.obj.toString());//接收客户端线程发来的Message对象,用来显示
}
}
/**
* 客户端线程。连到服务器:192.168.1.102:9998 发送消息,之后接收服务器消息,并在TextView显示
*
* @author S.Rain
*
*/
class ReceiveMessageThread extends Thread {
public void run() {
try {
Socket socket = new Socket("192.168.1.102", 9998);
DataOutputStream dos = new DataOutputStream(
socket.getOutputStream());
dos.writeUTF("您好,服务器!");
DataInputStream dis = new DataInputStream(
socket.getInputStream());
Message msg = mHandler.obtainMessage();
msg.obj = dis.readUTF();
mHandler.sendMessage(msg);
socket.close();
dos.close();
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {// 没用,工程创建时自己生成的
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
|