• android 与 PC的socket通信


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class YaoChatServer extends Thread {

    private YaoChatServer() throws IOException {
       CreateSocket();
       
    //创建Socket服务器


    public void run() { 
       Socket client;
       String txt;
       
    try { 
        
    while (true)
        
    //线程无限循环,实时监听socket端口
        {
         client
    =ResponseSocket();       
         
    //响应客户端链接请求。。
        
         
    while(true)
         {
          txt
    =ReceiveMsg(client);  
          System.out.println(txt);
          
    //链接获得客户端发来消息,并将其显示在Server端的屏幕上
       
          SendMsg(client,txt);
          
    //向客户端返回消息

          
    if(true)break;
          
    //中断,继续等待链接请求
         }
        
         CloseSocket(client);
         
    //关闭此次链接
        }   
       } 
       
    catch (IOException e) {
        System.out.println(e);
       }

    }

    private ServerSocket server = null;
    private static final int PORT = 5000;
    private BufferedWriter writer;
    private BufferedReader reader;

    private void CreateSocket() throws IOException
    {
       server 
    = new ServerSocket(PORT, 100);
       System.out.println(
    "Server starting..");  
    }

    private Socket ResponseSocket() throws IOException
    {
       Socket client 
    = server.accept(); 
       System.out.println(
    "client connected..");
      
       
    return client;
    }

    private void CloseSocket(Socket socket) throws IOException
    {
       reader.close();
       writer.close();
       socket.close();
       System.out.println(
    "client closed..");
    }

    private void SendMsg(Socket socket,String Msg) throws IOException 
    {
       writer 
    = new BufferedWriter(
          
    new OutputStreamWriter(socket.getOutputStream()));
        writer.write(Msg
    +"\n");
        writer.flush();
       
    }

    private String ReceiveMsg(Socket socket) throws IOException
    {
       reader 
    = new BufferedReader(
         
    new InputStreamReader(socket.getInputStream()));     
        System.out.println(
    "server get input from client socket..");
        String txt
    ="Sever send:"+reader.readLine();
       
        
    return txt;
    }

    public static void main(final String args[]) throws IOException {
       YaoChatServer yaochatserver 
    = new YaoChatServer();
       
    if (yaochatserver != null) {
        yaochatserver.start();
       }
    }

    }

    package com.android.Yao;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.net.UnknownHostException;

    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.Socket;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.*;


    public class YaoChatRoomAndroid extends Activity {
        
    /** Called when the activity is first created. */
        @Override
        
    public void onCreate(Bundle savedInstanceState) {
            
    super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           

            findviews();
            setonclick();

        }

        
    private EditText chattxt;
        
    private TextView chattxt2;
        
    private Button chatok;
        
        
    public void findviews()
        {
            chattxt 
    = (EditText)this.findViewById(R.id.chattxt);
            chattxt2 
    = (TextView)this.findViewById(R.id.chattxt2);
            chatok   
    = (Button)this.findViewById(R.id.chatOk);
        }
        
        
    private void setonclick()
        {
            chatok.setOnClickListener(
    new View.OnClickListener() {
       
        @Override
        
    public void onClick(View v) {
         
    try {
          connecttoserver(chattxt.getText().toString());
         } 
    catch (UnknownHostException e) {
          
    // TODO Auto-generated catch block
          e.printStackTrace();
         } 
    catch (IOException e) {
          
    // TODO Auto-generated catch block
          e.printStackTrace();
         }
        }
       });
        }


        
    public void connecttoserver(String socketData) throws UnknownHostException, IOException
    {
        Socket socket
    =RequestSocket("192.168.10.119",5000);
       SendMsg(socket,socketData);  
       String txt 
    = ReceiveMsg(socket);
       
    this.chattxt2.setText(txt);    
    }

        
        
    private Socket RequestSocket(String host,int port) throws UnknownHostException, IOException
        {   
        Socket socket 
    = new Socket(host, port);
        
    return socket;
        }
        
        
    private void SendMsg(Socket socket,String msg) throws IOException
        {
        BufferedWriter writer 
    = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
       writer.write(msg.replace(
    "\n"" ")+"\n");
       writer.flush();
        }
        
        
    private String ReceiveMsg(Socket socket) throws IOException
        {
        BufferedReader reader 
    = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        
       String txt
    =reader.readLine();
       
    return txt;

        }    
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation
    ="vertical"
        android:layout_width
    ="fill_parent"
        android:layout_height
    ="fill_parent"
        
    >
    <TextView
    android:id="@+id/chattxt2"
    android:layout_width
    ="319px"
    android:layout_height
    ="68px"
    android:text
    ="TextView"
    android:layout_alignParentTop
    ="true"
    android:layout_alignParentLeft
    ="true"
    >
    </TextView>
    <EditText
    android:id="@+id/chattxt"
    android:layout_width
    ="319px"
    android:layout_height
    ="52px"
    android:text
    ="EditText"
    android:textSize
    ="18sp"
    android:layout_below
    ="@+id/widget30"
    android:layout_alignParentLeft
    ="true"
    >
    </EditText>
    <Button
    android:id="@+id/chatOk"
    android:layout_width
    ="320px"
    android:layout_height
    ="41px"
    android:text
    ="Button"
    android:layout_below
    ="@+id/widget29"
    android:layout_alignParentLeft
    ="true"
    >
    </Button>

    </LinearLayout>
  • 相关阅读:
    java面向对象,封装和继承之电子宠物应用
    java之面向对象及定义一个计算器
    java之练习字符串的处理
    java编译变量的正确写法和控制台输入
    java基础类型及运算符
    JS之表单验证及职业素养
    JS实例之左侧菜单下拉效果,实现左侧菜单栏点击打开关闭效果
    JS实例之列表选中,实现类似好友列表选中效果
    idea激活码+方法 亲测有效
    恶意软件关不掉,老是弹出来
  • 原文地址:https://www.cnblogs.com/tt_mc/p/1751260.html
Copyright © 2020-2023  润新知