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