android之socket网络编程

最近在学习android关于网络编程的知识,在这个过程中也遇到了不少问题,这篇文章是android基于socket的网络编程,能让人更好的了解网络编程底层的相关知识。其他话就不说了,直接看代码吧。

服务器端采用的是图形化界面:

package com.app.qrs; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; class Myserver extends JFrame implements ActionListener{ public static final String Server_Message = "服务器发布的信息:"; private JTextField messagetext; private JButton ok; public static String servermsg = "hello android Client!"; public Myserver() { super("信息发布"); Container c = getContentPane(); JPanel panel = new JPanel(); JLabel lablename = new JLabel(Server_Message); messagetext = new JTextField(30); messagetext.addActionListener(this); panel.add(lablename); panel.add(messagetext); ok = new JButton("确定"); ok.addActionListener(this); panel.add(ok); c.add(panel); } public static ServerSocket server; /** * @param args */ public static void main(String[] args) { Myserver ms = new Myserver(); ms.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ms.setSize(280, 160); ms.setVisible(true); try { server = new ServerSocket(10000); System.out.println(InetAddress.getLocalHost()); while(true) { System.out.println("------------->"+servermsg); Socket socket = server.accept(); OutputStream out = socket.getOutputStream(); String msg = servermsg; out.write(msg.getBytes()); socket.close(); } }catch(Exception e) { e.printStackTrace(); } // TODO Auto-generated method stub } @Override public void actionPerformed(ActionEvent e) { /* * * 数据缓存解决了客服端存在中间一段黑屏的效果 * 即在客服端第一次点击刷新按钮时会有黑屏效果 * 第二次才能出现信息。 * 但在客服端还是存在bug(服务器未更新信息,客服端多次刷新的时候) * **/ String servermsgbuffer = new String(); servermsgbuffer=messagetext.getText(); servermsg = servermsgbuffer; } }

android客服端得代码:

package com.app.qrs; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TestSocketActivity extends Activity { private TextView mytextview; private Button Rebnt; Socket socket; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { mytextview = (TextView)findViewById(R.id.msgtextview); Rebnt = (Button)findViewById(R.id.reflesh); System.out.println("aaaa---------------->!"); System.out.println(InetAddress.getLocalHost()); /*** * 在socket所出现的问题是,用InetAddress.getloalhost()所得到的网址是不正确的, * 它的值和server的本地服务器是不一样的,所以要在socket里的第一个参数准确传入 * 服务器的地址。 * 解决方法: * 在服务器用Inetaddress.getlocalhost()方法获得地址。 * 将该地址放入客服端上socket的第一个参数 * ****/ socket = new Socket("192.168.1.103",10000); System.out.println(InetAddress.getLocalHost()+"---------------2"); InputStream in = socket.getInputStream(); //直接将流放入字节流 byte[] buffer = new byte[in.available()]; in.read(buffer); //String msg = new String(buffer); /** * 此句解决了在模拟器上的显示服务器上得到的中文流, * 将字节流utf-8的格式转换为gb2312的中文显示(个人体会) * ***/ String msg = new String(buffer,"gb2312"); /** * * 第二种读流方式,目前还没有找到此方法的中文显示方法 * * * ****/ // while(true) // { // StringBuffer sb = new StringBuffer(); // int c = 0; // while(((c=in.read())!='/n')&&(c!=-1)) // { // sb.append((char)c); // } // if(c==-1) // { // String str = new String(sb); // mytextview.setText(str); // break; // } // } mytextview.setText(msg); Rebnt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { socket = new Socket("192.168.1.103",10000); InputStream in = socket.getInputStream(); //直接将流放入字节流 byte[] buffer = new byte[in.available()]; in.read(buffer); String msg = new String(buffer,"gb2312"); mytextview.setText(msg); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); }catch(UnknownHostException e) { System.out.println(e.toString()); }catch(Exception e) { e.printStackTrace(); } } }

你可能感兴趣的:(android,socket,网络,String,服务器,buffer)