安卓套接字练习

安卓文件

[b]main.xml[/b]


<?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"
>
<EditText
android:layout_width="183dp"
android:layout_height="wrap_content"
android:id="@+id/editText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:onClick="sendMessage"
android:id="@+id/button" />
</LinearLayout>


[b]AndroidManifest.xml[/b]

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.taojiezi"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" android:maxSdkVersion="18"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
[b]<uses-permission android:name="android.permission.INTERNET" />[/b]
</manifest>


客户端

package com.example.taojiezi;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class MyActivity extends Activity {
EditText editText;
Button button;
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText)this.findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
textView =(TextView)findViewById(R.id.textView);
}
public void sendMessage(View view){
String sendText = editText.getText().toString();
Socket s = null;
DataOutputStream dout = null;
DataInputStream din = null;
try {
s = new Socket("192.168.20.139",8888);
dout = new DataOutputStream(s.getOutputStream());
din = new DataInputStream(s.getInputStream());
dout.writeUTF(sendText);
textView.setText("服务器发来的消息:"+din.readUTF());
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
dout.close();
din.close();
s.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}


服务器

package com.sinitek.sirm.busin.investpool.service.workflow;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
* Created by IntelliJ IDEA.
* User: sinitek
* Date: 14-2-7
* Time: 上午9:51
* To change this template use File | Settings | File Templates.
*/
public class Server {
public static void main(String[] args){
ServerSocket aServerSocket = null; // Server Socet.
Socket aSessionSoket = null; // Server Socet session.
DataInputStream aDataInput = null; // Server input Stream that to receive msg from client.
DataOutputStream aDataOutput = null; // Server output Stream that to send data to client.
BufferedReader aBufferReader = null; //
try {
aServerSocket = new ServerSocket(8888); // listen 8888 port.
System.out.println("already listen 8888 port.");
} catch (Exception e) {
e.printStackTrace();
}

while(true){
try {
aSessionSoket = aServerSocket.accept(); //accept client msg.
aDataInput = new DataInputStream(aSessionSoket.getInputStream());
aDataOutput = new DataOutputStream(aSessionSoket.getOutputStream());
String msg = aDataInput.readUTF(); //read msg.
System.out.println("ip: "+aSessionSoket.getInetAddress());//ip.
System.out.println("receive msg: "+msg);

// aBufferReader = new BufferedReader(new InputStreamReader(System.in));
String str = null;
// System.out.println("Enter your send value:");
// str = aBufferReader.readLine();
aDataOutput.writeUTF("服务器输出:"+msg);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
aDataInput.close();
aDataOutput.close();
aBufferReader.close();
aSessionSoket.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值