Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207935
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-08 11:44:04

01_24_Socket编程








点击(此处)折叠或打开

  1. //SocketActivity.java 服务器端
  2. package mars.socket;

  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.DatagramPacket;
  6. import java.net.DatagramSocket;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;

  9. import android.app.Activity;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;

  14. public class SocketActivity extends Activity {
  15.     /** Called when the activity is first created. */
  16.     private Button startButton = null;
  17.     @Override
  18.     public void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.main);
  21.         startButton = (Button)findViewById(R.id.startListener);
  22.         startButton.setOnClickListener(new StartSocketListener());
  23.     }
  24.     
  25.     class StartSocketListener implements OnClickListener{

  26.         @Override
  27.         public void onClick(View v) {
  28.             System.out.print("111111111Start*************");
  29.             new ServerThread().start();
  30.         }
  31.         
  32.     }
  33.     
  34.     class ServerThread extends Thread{
  35.         /* TCP
  36.          * 1、创建一个服务SOCKET: serverSocket = new ServerSocket(port);
  37.          * 2、接受客户端发送的请求: socket = serverSocket.accept()
  38.          * 3、取出socket中的输入流: inputSteam = socket.getINputStream()
  39.          * 4、把输入流读到字节流中 temp = imputSteam.read(buffer);
  40.          * 5、把字节流转换为字符串:str = new String(buffer,0,temp);
  41.          * */
  42.         public void run(){
  43.             //声明一个ServerSocket对象
  44.             System.out.print("1111111111run*******");
  45.             
  46.             ServerSocket serverSocket = null;
  47.             try {
  48.                 //创建一个ServerSocket对象,并让这个Socket在4567端口监听
  49.                 serverSocket = new ServerSocket(4570);
  50.                 System.out.print("new ServerSocket(4567)*******");
  51.                 //调用ServerSocket的accept()方法,接受客户端所发送的请求
  52.                 Socket socket = serverSocket.accept();
  53.                 System.out.print("1111111111serverSocket.accept()");
  54.                 //从Socket当中得到InputStream对象
  55.                 InputStream inputStream = socket.getInputStream();
  56.                 System.out.print("1111111111socket.getInputStream()");
  57.                 byte buffer [] = new byte[1024*4];
  58.                 int temp = 0;
  59.                 //从InputStream当中读取客户端所发送的数据
  60.                 while((temp = inputStream.read(buffer)) != -1){
  61.                     System.out.println(new String(buffer,0,temp));
  62.                 }
  63.                 System.out.print("1111111111 end");
  64.             } catch (IOException e) {
  65.                 // TODO Auto-generated catch block
  66.                 e.printStackTrace();
  67.             }
  68.             finally{
  69.                 try {
  70.                     serverSocket.close();
  71.                 } catch (IOException e) {
  72.                     // TODO Auto-generated catch block
  73.                     e.printStackTrace();
  74.                 }
  75.             }
  76.         
  77.         }
  78.         
  79.         /*UDP
  80.          * 1、创建一个报文socket,并指定监听端口号 DatagramSocket socket = new DatagramSocket(4567);
  81.          * 2、创建一个字节数组 data[] = new byte[1024];
  82.          * 3、创建一个报文包DatagreamPacket packet = new DatagramPacket(data,data.length);指定接收到的数据放在data[]上
  83.          * 4、使用DatagramSocket的receive(packet)接收数据包
  84.          * 5、从packet报文包中去数据 packet.getData()
  85.          */
  86.         
  87.  
  88.         /*public void run(){
  89.             try {
  90.                 //创建一个DatagramSocket对象,并指定监听的端口号
  91.                 System.out.print("1111111111run*******");
  92.                 DatagramSocket socket = new DatagramSocket(4567);
  93.                 
  94.                 System.out.print("1111111111socket*************");
  95.                 
  96.                 byte data [] = new byte[1024];
  97.                 //创建一个空的DatagramPacket对象
  98.                 DatagramPacket packet = new DatagramPacket(data,data.length);
  99.                 System.out.print("1111111111packet = new DatagramPacket(data,data.length)********");
  100.                 //使用receive方法接收客户端所发送的数据
  101.                 socket.receive(packet);
  102.                 System.out.print("1111111111111socket.receive(packet)*******");
  103.                 String result = new String(packet.getData(),packet.getOffset(),packet.getLength());
  104.                 System.out.print("1111111packet.getData()*********");
  105.                 System.out.println("111result--->" + result);
  106.             } catch (Exception e) {
  107.                 System.out.print("catch (Exception e)*************");
  108.                 e.printStackTrace();
  109.             }
  110.         }*/
  111.     }
  112. }

点击(此处)折叠或打开

  1. //TCPClient.java TCP客户端 这里是一个java程序
  2. package mars;

  3. import java.io.FileInputStream;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7. import java.io.Writer;
  8. import java.net.Socket;

  9. public class TCPClient {
  10.     public static void main(String[] args) {
  11.         /*TCP客户端
  12.          * 1、创建socket: socket = new Socket("IP",port);
  13.          * 2、得到文件的输入流:inputSteam = new FileInputStream("f://file/words.txt");
  14.          * 3、得到socket的输出流:socket.getOutputSteam()
  15.          * 4、定义字节数字
  16.          * 5、从输入流中读取数据到字节数组中:temp = inputSteam.read(buffer)
  17.          * 6、把字节数组中的数据写到socket输出流中:outputStream.write(buffer,0,temp);
  18.          * */
  19.         try {
  20.             //创建一个Socket对象,指定服务器端的IP地址和端口号
  21.             Socket socket = new Socket("172.18.2.164",4570);
  22.             System.out.print(" 222222222Socket72.18.2.164,4567******");
  23.             //使用InputStream读取硬盘上的文件
  24.             InputStream inputStream = new FileInputStream("f://file/words.txt");
  25.             System.out.print("222222FileInputStream(");
  26.             //从Socket当中得到OutputStream
  27.             OutputStream outputStream = socket.getOutputStream();
  28.             System.out.print("22222 socket.getOutputStream()");
  29.             byte buffer [] = new byte[4*1024];
  30.             int temp = 0 ;
  31.             //将InputStream当中的数据取出,并写入到OutputStream当中
  32.             while((temp = inputStream.read(buffer)) != -1){
  33.                 outputStream.write(buffer, 0, temp);
  34.             }
  35.             System.out.print("222222 end ");
  36.             outputStream.flush();
  37.         } catch (Exception e) {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41. }


点击(此处)折叠或打开

  1. //UDPClient.java UDP客户端 这里是一个java程序
  2. package mars;

  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;

  6. public class UDPClient {
  7.     /*UDP
  8.      * 1、创建一个报文流对象:DatagreamSocket socket = new DatagreamSocket(4567);并指定端口号
  9.      * 2、创建一个InetAddress 指向服务器的地址:InetAddress serverAddress = InetAddress.getByName("172.2.18.164");
  10.      * 3、定义一个字符串,并转换为字节 byte data[] = str.getBytes()
  11.      * 4、创建一个数据报包 DatagramPacket对象,表配置
  12.      *         DatagreamPacket packet = new DatagramPacket(data,data.length,serverAddress,4567);
  13.      *     其中data为要发送的数据,serverAddress表服务器的地址,4567表端口号
  14.      * 5、通过socket的send方法发送数据socket.send(packet)
  15.      * */
  16.     public static void main(String[] args) {
  17.         try {
  18.             //首先创建一个DatagramSocket对象
  19.             DatagramSocket socket = new DatagramSocket(4567);
  20.             System.out.print("222222222DatagramSocket socket = new DatagramSocket(4567);#######");
  21.             //创建一个InetAddree
  22.             InetAddress serverAddress = InetAddress.getByName("172.18.2.164");
  23.             System.out.print("222222222InetAddress serverAddress = InetAddress.getByNam#######");
  24.             String str = "hello";
  25.             byte data [] = str.getBytes();
  26.             //创建一个DatagramPacket对象,并指定要讲这个数据包发送到网络当中的哪个地址,以及端口号
  27.             DatagramPacket packet = new DatagramPacket(data,data.length,serverAddress,4567);
  28.             System.out.print(" 222222new DatagramPacket(data,data.length,serverAddress,4567);#######");
  29.             //调用socket对象的send方法,发送数据
  30.             socket.send(packet);
  31.             System.out.print("22222222socket.send(packet#######");
  32.         } catch (Exception e) {
  33.             // TODO Auto-generated catch block
  34.             e.printStackTrace();
  35.         }
  36.     }
  37. }


阅读(544) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~