android部分代码如下:

注意:在Manifest.xml中需要添加网络访问许可<uses-permission android:name="android.permission.INTERNET" />

package com.test.socket;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

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 SocketActivity extends Activity implements OnClickListener {

/** Called when the activity is first created. */

private TextView tv;

private Button btn, btn_exit;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tv = (TextView)findViewById(R.id.message);

tv.setText("Ready");

btn = (Button)findViewById(R.id.send);

btn.setOnClickListener(this);

btn_exit = (Button)findViewById(R.id.exit);

btn_exit.setOnClickListener(this);

}

public void onClick(View v) {

if (v == btn) {

tv.setText("Start");

int port = 7100;

try {

Socket socket = new Socket("10.10.15.79", port);

OutputStream out = socket.getOutputStream();

DataOutputStream dout = new DataOutputStream(out);

dout.writeUTF("Jordan");

InputStream in = socket.getInputStream();

DataInputStream din = new DataInputStream(in);

String str = din.readUTF();

tv.setText(str);

in.close();

out.close();

socket.close();

}catch (UnknownHostException e) {

// TODO: handle exception

tv.setText("UnknownHostError");

}catch (IOException e) {

// TODO: handle exception

tv.setText("IOError");

}

} else if (v == btn_exit) {

finish();

}

}

}

PC机端使用Java作为服务器,创建ServerSocket,之后等待客户端连接。具体代码如下:

import java.net.*;

import java.io.*;

public class SocketAppServer {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ServerSocket ss = null;

int port = 7100;

String hello="From Server:Hello World!";

//create ServerSocket

try {

ss = new ServerSocket(port);

}catch (IOException e) {

// TODO: handle exception

System.out.println(e);

System.exit(1);

}

//accept client connection

while(true) {

try {

System.out.println("Waiting for connection on port="+port);

Socket cs = ss.accept();

InputStream input = cs.getInputStream();

DataInputStream din = new DataInputStream(input);

String name = din.readUTF();

OutputStream output = cs.getOutputStream();

DataOutputStream dout = new DataOutputStream(output);

dout.writeUTF(hello+"Your name:"+name);

System.out.println("Connection OK port="+port);

System.out.println("=========================");

din.close();

output.close();

cs.close();

}catch (IOException e) {

// TODO: handle exception

System.out.println(e);

}

}

}

}

更多相关文章

  1. android apk 升级代码
  2. Android/iso客户端与服务器安全通信(Oauth2.0+3DES+RSA)
  3. Android 相机2之常用工具代码(预览方向、预览尺寸、全屏显示、分
  4. [置顶] Android防火墙+流量统计代码
  5. Android应用程序获取ROOT权限代码
  6. android 个人铃声设置代码
  7. android典型代码系列(九)------电话拦截
  8. Android里用代码设置View的相关属性
  9. Android自动测试代码

随机推荐

  1. android 应用异常可以引起android系统崩
  2. Android: How to play Tone sound
  3. Android(安卓)文件下载工具类
  4. android 源码编译报错记录
  5. 【Android策略】The content of the adap
  6. Android(安卓)动画1--View控件的显示和隐
  7. Android关于图片处理,更改图片灰度
  8. android 银联支付接入报nullexception异
  9. android之service
  10. Android手机中紧急号码的定制