package com.socket.client;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class main extends Activity implements OnClickListener{
/** Called when the activity is first created. */
//定义声明需要用到的UI元素
private EditText edtmsgcontent;
private Button btnSend;
private String ip="169.254.191.14";
private int port=1818;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
InitView();
}
private void InitView()
{
//显示主界面
setContentView(R.layout.main);
//通过id获取ui元素对象
edtmsgcontent=(EditText)findViewById(R.id.msgcontent);
btnSend=(Button)findViewById(R.id.btnsend);
//为btnsend设置点击事件
btnSend.setOnClickListener(this);
}
public void onClick(View bt)
{
try
{
String msg=edtmsgcontent.getText().toString();
if(!TextUtils.isEmpty(msg))
SendMsg(ip,port,msg);
else
{
Toast.makeText(this,"请先输入要发送的内容", Toast.LENGTH_LONG);
edtmsgcontent.requestFocus();
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void SendMsg(String ip,int port,String msg) throws UnknownHostException, IOException
{
try
{
Socket socket=null;
socket=new Socket(ip,port);
BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(msg);
writer.flush();
writer.close();
socket.close();
}
catch(UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android+Socket编程源码(与PC通讯).rar (27个子文件)
Socket_Client
.project 849B
proguard.cfg 1KB
src
com
socket
client
main.java 2KB
AndroidManifest.xml 796B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
main.xml 673B
values
strings.xml 197B
assets
gen
com
socket
client
R.java 841B
default.properties 362B
.classpath 280B
bin
resources.ap_ 11KB
com
socket
client
R$layout.class 391B
R$drawable.class 397B
R$attr.class 337B
main.class 3KB
R$id.class 416B
R.class 524B
R$string.class 452B
classes.dex 4KB
Socket_Client.apk 15KB
Socket_Server
.project 389B
src
TCPDesktopServer.java 1KB
.settings
org.eclipse.jdt.core.prefs 629B
.classpath 301B
bin
TCPDesktopServer.class 2KB
共 27 条
- 1
chs626419
- 粉丝: 1
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页