android客户端 PC服务端

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Android客户端Socket连接PC服务器端(转载加评论)文章分类:JavaEye先上代码:1.SocketConnect.java文件Java代码1.packagecn.com;2.3.importjava.io.BufferedReader;4.importjava.io.BufferedWriter;5.importjava.io.InputStreamReader;6.importjava.io.OutputStreamWriter;7.importjava.io.PrintWriter;8.importjava.net.Socket;9.10.importandroid.app.Activity;11.importandroid.os.Bundle;12.importandroid.util.Log;13.importandroid.view.View;14.importandroid.view.View.OnClickListener;15.importandroid.widget.Button;16.importandroid.widget.EditText;17.importandroid.widget.TextView;18.19.publicclassSocketConnectextendsActivity{20.21.privateTextViewmTextView=null;22.privateEditTextmEditText=null;23.privateButtonmButton=null;24.25./**Calledwhentheactivityisfirstcreated.*/26.@Override27.publicvoidonCreate(BundlesavedInstanceState){28.super.onCreate(savedInstanceState);29.setContentView(R.layout.main);30.31.mButton=(Button)findViewById(R.id.Button01);32.mTextView=(TextView)findViewById(R.id.TextView01);33.mEditText=(EditText)findViewById(R.id.EditText01);34.35.//登陆36.mButton.setOnClickListener(newOnClickListener(){37.publicvoidonClick(Viewv){38.Socketsocket=null;39.Stringmessage=mEditText.getText().toString()+\r\n;40.try{41.//创建Socket42.socket=newSocket(192.168.1.100,5554);//查看本机IP,每次开机都不同43.//向服务器发送消息44.PrintWriterout=newPrintWriter(newBufferedWriter(45.newOutputStreamWriter(socket.getOutputStream())),46.true);47.out.println(message);48.49.//接收来自服务器的消息50.BufferedReaderbr=newBufferedReader(51.newInputStreamReader(socket.getInputStream()));52.Stringmsg=br.readLine();53.54.if(msg!=null){55.mTextView.setText(msg);56.}else{57.mTextView.setText(数据错误!);58.}59.//关闭流60.out.close();61.br.close();62.//关闭Socket63.socket.close();64.}catch(Exceptione){65.//TODO:handleexception66.Log.e(,e.toString());67.}68.}69.});70.}71.}2.main.xml文件简单布局Java代码1.?xmlversion=1.0encoding=utf-8?2.LinearLayoutxmlns:android=:orientation=verticalandroid:layout_width=fill_parent4.android:layout_height=fill_parent5.Buttonandroid:text=Button01android:id=@+id/Button016.android:layout_width=wrap_contentandroid:layout_height=wrap_content/Button7.TextViewandroid:text=TextView01android:id=@+id/TextView018.android:layout_width=wrap_contentandroid:layout_height=wrap_content/TextView9.EditTextandroid:text=EditText01android:id=@+id/EditText0110.android:layout_width=wrap_contentandroid:layout_height=wrap_content/EditText11./LinearLayout3.注意添加AndroidMenifest.xml的权限Java代码1.uses-permissionandroid:name=android.permission.INTERNET/4.服务器端,就是建立一个简单的java文件,运行起来就可以了Java代码1.packagecn.com;2.3.importjava.io.BufferedReader;4.importjava.io.BufferedWriter;5.importjava.io.InputStreamReader;6.importjava.io.OutputStreamWriter;7.importjava.io.PrintWriter;8.importjava.net.ServerSocket;9.importjava.net.Socket;10.11.publicclassServerimplementsRunnable{12.publicvoidrun(){13.try{14.//创建ServerSocket15.ServerSocketserverSocket=newServerSocket(5554);16.while(true){17.//接受客户端请求18.Socketclient=serverSocket.accept();19.System.out.println(accept);20.try{21.//接收客户端消息22.BufferedReaderin=newBufferedReader(23.newInputStreamReader(client.getInputStream()));24.Stringstr=in.readLine();25.System.out.println(read:+str);26.//向服务器发送消息27.PrintWriterout=newPrintWriter(newBufferedWriter(28.newOutputStreamWriter(client.getOutputStream())),29.true);30.out.println(servermessage);31.//关闭流32.out.close();33.in.close();34.}catch(Exceptione){35.System.out.println(e.getMessage());36.e.printStackTrace();37.}finally{38.//关闭39.client.close();40.System.out.println(close);41.}42.}43.}catch(Exceptione){44.System.out.println(e.getMessage());45.}46.}47.48.//main函数,开启服务器49.publicstaticvoidmain(Stringa[]){50.ThreaddesktopServerThread=newThread(newServer());51.desktopServerThread.start();52.}53.}以上即为所以代码,希望大家有所收获...感谢志毅的blog啊,之前写过差不多的代码没有调试成功还....呵呵[其他难题]Android客户端连接PC服务器端(Socket连接)[复制链接]客户端代码:packagecn.com;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.Socket;importandroid.app.Activity;importandroid.os.Bundle;importandroid.widget.Button;importandroid.widget.TextView;importandroid.view.View;importandroid.view.View.OnClickListener;publicclassSocketClientextendsActivityimplementsOnClickListener{Socketsocket;DataInputStreamdis;DataOutputStreamdos;privateTextViewmTextView1;privateButtonButton01;/**Calledwhentheactivityisfirstcreated.*/@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);mTextView1=(TextView)findViewById(R.id.rec);Button01=(Button)findViewById(R.id.Button01);Button01.setOnClickListener(this);Client(127.0.0.1);//WriteInt(25);//WriteString(client);//ReadInt();}publicvoidClient(StringIP){try{//specifiedbytheparametersdstNameanddstPort.//创建一个socket流连接到目标主机,socket=newSocket(IP,10000);//输入流读出数据输出流写数据dis=newDataInputStream(socket.getInputStream());dos=newDataOutputStream(socket.getOutputStream());}catch(IOExceptionioe){ioe.printStackTrace();}}//写数据到socketpublicvoidWriteInt(inti){try{dos.wr

1 / 14
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功