cocos2d-x中有没有tcp/udp这样的网络通信方式

&&国之画&&布布分享&&&& &&&&
版权所有 京ICP备号-2
迷上了代码!君,已阅读到文档的结尾了呢~~
基于cocos2d-x的游戏框架..
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
基于cocos2d-x的游戏框架设计
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口android 网络编程之网络通信几种方式实例分享
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了android 网络编程之网络通信几种方式,有需要的朋友可以参考一下
如今,手机应用渗透到各行各业,数量难以计数,其中大多数应用都会使用到网络,与服务器的交互势不可挡,那么android当中访问网络有哪些方式呢?
现在总结了六种方式:
(1)针对TCP/IP的Socket、ServerSocket
(2)针对UDP的DatagramSocket、DatagramPackage。这里需要注意的是,考虑到Android设备通常是手持终端,IP都是随着上网进行分配的。不是固定的。因此开发也是有一点与普通互联网应用有所差异的。
(3)针对直接URL的HttpURLConnection。
(4)Google集成了Apache HTTP客户端,可使用HTTP进行网络编程。
(5)使用WebService。Android可以通过开源包如jackson去支持Xmlrpc和Jsonrpc,另外也可以用Ksoap2去实现Webservice。
(6)直接使用WebView视图组件显示网页。基于WebView 进行开发,Google已经提供了一个基于chrome-lite的Web浏览器,直接就可以进行上网浏览网页。
一、socket与serverSocket
客户端代码
代码如下:public class TestNetworkActivity extends Activity implements OnClickListener{&private Button connectB&private Button sendB&private TextView showV&private EditText msgT&private S&private H&@Override&protected void onCreate(Bundle savedInstanceState) {&&super.onCreate(savedInstanceState);
&&setContentView(R.layout.test_network_main);
&&connectBtn = (Button) findViewById(R.id.test_network_main_btn_connect);&&sendBtn = (Button) findViewById(R.id.test_network_main_btn_send);&&showView = (TextView) findViewById(R.id.test_network_main_tv_show);&&msgText = (EditText) findViewById(R.id.test_network_main_et_msg);&&connectBtn.setOnClickListener(this);&&sendBtn.setOnClickListener(this);&&handler = new Handler(){&&&@Override&&&public void handleMessage(Message msg) {&&&&super.handleMessage(msg);&&&&String data = msg.getData().getString("msg");&&&&showView.setText("来自服务器的消息:"+data);&&&}&&};&}&@Override&public void onClick(View v) {&&//连接服务器&&if(v == connectBtn){&&&connectServer();&&}&&//发送消息&&if(v == sendBtn){&&&String msg = msgText.getText().toString();&&&send(msg);&&}&}&&&&&& /** & *连接服务器的方法& */&public void connectServer(){&&try {&&&socket = new Socket("192.168.1.100",4000);&&&System.out.println("连接服务器成功");&&&recevie();&&} catch (Exception e) {&&&System.out.println("连接服务器失败"+e);&&&e.printStackTrace();&&} &}&&&&&& /**& *发送消息的方法& */&public void send(String msg){&&try {&&&PrintStream ps = new PrintStream(socket.getOutputStream());&&&ps.println(msg);&&&ps.flush();&&} catch (IOException e) {&&&e.printStackTrace();&&}&}&&&&&& /**& *读取服务器传回的方法& */&public void recevie(){
&&new Thread(){&&&public void run(){&&&&while(true){&&&&&try {&&&&&&InputStream is = socket.getInputStream();&&&&&&BufferedReader br = new BufferedReader(new InputStreamReader(is));&&&&&&String str = br.readLine();&&&&&&Message message = new Message();&&&&&&Bundle bundle = new Bundle();&&&&&&bundle.putString("msg", str);&&&&&&message.setData(bundle);&&&&&&handler.sendMessage(message);&&&&&} catch (IOException e) {&&&&&&e.printStackTrace();&&&&&}&&&&}&&&}&&}.start();&}}
二、RUL、URLConnection、httpURLConnection、ApacheHttp、WebView 代码如下:public class TestURLActivity extends Activity implements OnClickListener {&private Button connectB&private Button urlConnectionB&private Button httpUrlConnectionB&private Button httpClientB&private ImageView showImageV&private TextView showTextV&private WebView webV
&@Override&protected void onCreate(Bundle savedInstanceState) {&&super.onCreate(savedInstanceState);&&setContentView(R.layout.test_url_main);
&&connectBtn = (Button) findViewById(R.id.test_url_main_btn_connect);&&urlConnectionBtn = (Button) findViewById(R.id.test_url_main_btn_urlconnection);&&httpUrlConnectionBtn = (Button) findViewById(R.id.test_url_main_btn_httpurlconnection);&&httpClientBtn = (Button) findViewById(R.id.test_url_main_btn_httpclient);&&showImageView = (ImageView) findViewById(R.id.test_url_main_iv_show);&&showTextView = (TextView) findViewById(R.id.test_url_main_tv_show);&&webView = (WebView) findViewById(R.id.test_url_main_wv);&&connectBtn.setOnClickListener(this);&&urlConnectionBtn.setOnClickListener(this);&&httpUrlConnectionBtn.setOnClickListener(this);&&httpClientBtn.setOnClickListener(this);&}
&@Override&public void onClick(View v) {&&// 直接使用URL对象进行连接&&if (v == connectBtn) {&&&try {&&&&URL url = new URL("http://192.168.1.100:8080/myweb/image.jpg");&&&&InputStream is = url.openStream();&&&&Bitmap bitmap = BitmapFactory.decodeStream(is);&&&&showImageView.setImageBitmap(bitmap);&&&} catch (Exception e) {&&&&e.printStackTrace();&&&}
&&}&&// 直接使用URLConnection对象进行连接&& &&if (v == urlConnectionBtn) {&&&try {&&&&URL url = new URL("http://192.168.1.100:8080/myweb/hello.jsp");&&&&URLConnection connection = url.openConnection();&&&&InputStream is = connection.getInputStream();&&&&byte[] bs = new byte[1024];&&&&int len = 0;&&&&StringBuffer sb = new StringBuffer();&&&&while ((len = is.read(bs)) != -1) {&&&&&String str = new String(bs, 0, len);&&&&&sb.append(str);&&&&}&&&&showTextView.setText(sb.toString());&&&} catch (Exception e) {&&&&e.printStackTrace();&&&}
&&}&&// 直接使用HttpURLConnection对象进行连接&&if (v == httpUrlConnectionBtn) {&&&try {&&&&URL url = new URL(&&&&&&"http://192.168.1.100:8080/myweb/hello.jsp?username=abc");&&&&HttpURLConnection connection = (HttpURLConnection) url&&&&&&.openConnection();&&&&connection.setRequestMethod("GET");&&&&if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {&&&&&String message = connection.getResponseMessage();&&&&&showTextView.setText(message);&&&&}&&&} catch (Exception e) {&&&&e.printStackTrace();&&&}&&}&&// 使用ApacheHttp客户端进行连接(重要方法)&&if (v == httpClientBtn) {&&&try {&&&&HttpClient client = new DefaultHttpClient();&&&&// 如果是Get提交则创建HttpGet对象,否则创建HttpPost对象&&&&// HttpGet httpGet = new&&&&// HttpGet("http://192.168.1.100:8080/myweb/hello.jsp?username=abc&pwd=321");&&&&// post提交的方式&&&&HttpPost httpPost = new HttpPost(&&&&&&"http://192.168.1.100:8080/myweb/hello.jsp");&&&&// 如果是Post提交可以将参数封装到集合中传递&&&&List dataList = new ArrayList();&&&&dataList.add(new BasicNameValuePair("username", "aaaaa"));&&&&dataList.add(new BasicNameValuePair("pwd", "123"));&&&&// UrlEncodedFormEntity用于将集合转换为Entity对象&&&&httpPost.setEntity(new UrlEncodedFormEntity(dataList));&&&&// 获取相应消息&&&&HttpResponse httpResponse = client.execute(httpPost);&&&&// 获取消息内容&&&&HttpEntity entity = httpResponse.getEntity();&&&&// 把消息对象直接转换为字符串&&&&String content = EntityUtils.toString(entity);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //showTextView.setText(content);&&&&//通过webview来解析网页&&&&webView.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);&&&&//给点url来进行解析&&&&//webView.loadUrl(url);&&&} catch (ClientProtocolException e) {&&&&e.printStackTrace();&&&} catch (IOException e) {&&&&e.printStackTrace();&&&}&&}&}
三、使用webService 代码如下:public class LoginActivity extends Activity implements OnClickListener{&private Button loginB&private static final String SERVICE_URL = "http://192.168.1.100:8080/loginservice/LoginServicePort";&private static final String NAMESPACE = "/";&@Override&protected void onCreate(Bundle savedInstanceState) {&&super.onCreate(savedInstanceState);&&setContentView(R.layout.login_main);&&loginBtn = (Button) findViewById(R.id.login_main_btn_login);&&loginBtn.setOnClickListener(this);&}&@Override&public void onClick(View v) {&&if(v == loginBtn){&&&//创建WebService的连接对象&&&HttpTransportSE httpSE = new HttpTransportSE(SERVICE_URL);&&&//通过SOAP1.1协议对象得到envelop&&&SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);&&&//根据命名空间和方法名来创建SOAP对象&&&SoapObject soapObject = new SoapObject(NAMESPACE, "validate");&&&//向调用方法传递参数&&&soapObject.addProperty("arg0", "abc");&&&soapObject.addProperty("arg1","123");&&&//将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息&&&envelop.bodyOut = soapO&&&try {&&&&//开始调用远程的方法&&&&httpSE.call(null, envelop);&&&&//得到远程方法返回的SOAP对象&&&&SoapObject resultObj = (SoapObject) envelop.bodyIn;&&&&//根据名为return的键来获取里面的值,这个值就是方法的返回值&&&&String returnStr = resultObj.getProperty("return").toString();&&&&Toast.makeText(this, "返回值:"+returnStr, Toast.LENGTH_LONG).show();&&&} catch (IOException e) {&&&&e.printStackTrace();&&&} catch (XmlPullParserException e) {&&&&e.printStackTrace();&&&}&&}&}}
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 tcp udp区别 的文章

 

随机推荐