正文  网络通信 > Socket >

简单的Socket通信 手机与pc服务器通信

[代码] 手机客户端package com.newsocket;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.InputStreamR......

 
[代码] 手机客户端
 
package com.newsocket;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
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.util.Log;
import android.view.View;
import android.widget.*;
public class Activity01 extends Activity {
    private final String DEBUG_TAG = "Activity01";
    private TextView  mTextView = null;
    private EditText mEditText = null;
    private EditText mima = null;
    private Button mButton = null;
    private DataOutputStream toserver;
private DataInputStream fromserver;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton = (Button) this.findViewById(R.id.button1);
        mButton.setText("登录");
        mTextView = (TextView) this.findViewById(R.id.textView1);        
        mEditText = (EditText) this.findViewById(R.id.editText1);
        mima =(EditText) this.findViewById(R.id.editText2);
        mButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
      Socket socket = null;
      String ms = mEditText.getText().toString();
      String message = mima.getText().toString()+"\r\n";
      try{
    socket = new Socket("192.168.1.100",54321);
        //接受服务器的数据流
        fromserver = new DataInputStream( socket.getInputStream());
        //要发送的数据流
        toserver=new DataOutputStream(socket.getOutputStream());
        toserver.writeBytes(message);
        toserver.flush();
        String msg = fromserver.readLine();
      if(msg != null)
      {   if(msg.equals(ms))
      mTextView.setText("登录成功");
      else
      mTextView.setText("密码错误");
      }else
      {
      mTextView.setText("数据错误");
      
      }
      toserver.close();
      fromserver.close();
      socket.close();
      }catch(Exception e){
     mTextView.setText("没连接上");
      }
 
}
       
        });
    }
}
 
[代码] pc简单的服务器
 
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 AndroidServer implements Runnable{
 
@Override
public void run() {
  try{
  ServerSocket serverSocket = new ServerSocket(54321);
  while(true){
  System.out.println("Android socket 等待接受客户的连接");
  Socket client = serverSocket.accept();//接受客户端请求
  try{
  BufferedReader in = new BufferedReader
  (new InputStreamReader(client.getInputStream()));
  String str = in.readLine();
  System.out.println("read:"+str);
  PrintWriter out = new PrintWriter
  (new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true
  );
  out.println(str);
  in.close();
  out.close();
  }catch(Exception e){
  System.out.println(e.getMessage());
  e.printStackTrace();
  }
  finally{
  client.close();
  System.out.println("Android socket close");
  }
  
  }
  }catch(Exception e){
  System.out.println(e.getMessage());
  }
 
}
public static void main(String[] srgs){
Thread desktopServerThread = new Thread(new AndroidServer());
desktopServerThread.start();
}
 
 
}
 
[代码] 别忘了在AndroieManifest.xml里加上这个
 
<uses-permission android:name="android.permission.INTERNET"></uses-permission>