android  scoket

android client code:

 

//Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
package com.android.testandroid1;

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 Testandroid1Activity extends Activity {
   
    @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.editText1);
        chattxt2 = (TextView)this.findViewById(R.id.textView1);
        chatok   = (Button)this.findViewById(R.id.button1);
    }
   
    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("10.0.2.2",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;

     
}

 

pc  server  code:

 

//Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
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 testpc1 extends Thread {

private testpc1() 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.."+reader.readLine());
    String txt="Sever send:"+"server";
  
    return txt;
}

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值