㈠ 求給一個安卓小程序的代碼 就是單擊按鈕然後就與指定埠建立連接
我之前寫過一個socket的demo,你參考一下來做吧,我這個寫得很亂。
------------------------------------------------------------------------------
java代碼:
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.PrintStream;
importjava.io.UnsupportedEncodingException;
importjava.net.InetSocketAddress;
importjava.net.Socket;
importjava.net.UnknownHostException;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.Toast;
{
publicstaticfinalStringTAG=SocketTestActivity.class.getSimpleName();
/*伺服器地址*/
privateStringhost_ip=null;
/*伺服器埠*/
privateinthost_port=0;
privateButtonbtnConnect;
privateButtonbtnSend;
privateEditTexteditSend;
privateEditTexthostIP;
privateEditTexthostPort;
privateSocketsocket;
privatePrintStreamoutput;
privateStringbuffer="";
privateContextcontext;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_socket_test);
context=this;
initView();
btnConnect.setOnClickListener(newButton.OnClickListener(){
@Override
publicvoidonClick(Viewv){
host_ip=hostIP.getText().toString();
host_port=Integer.parseInt(hostPort.getText().toString());
newThread(newConnectThread()).start();
}
});
btnSend.setOnClickListener(newButton.OnClickListener(){
@Override
publicvoidonClick(Viewv){
newThread(newSendThread(editSend.getText().toString())).start();
}
});
}
privatevoidtoastText(Stringmessage){
Toast.makeText(context,message,Toast.LENGTH_LONG).show();
}
publicvoidhandleException(Exceptione,Stringprefix){
e.printStackTrace();
toastText(prefix+e.toString());
}
publicvoidinitView(){
btnConnect=(Button)findViewById(R.id.btnConnect);
btnSend=(Button)findViewById(R.id.btnSend);
editSend=(EditText)findViewById(R.id.sendMsg);
hostIP=(EditText)findViewById(R.id.hostIP);
hostPort=(EditText)findViewById(R.id.hostPort);
}
privatevoidcloseSocket(){
try{
output.close();
socket.close();
}catch(IOExceptione){
handleException(e,"closeexception:");
}
}
Handlerhandler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg){
super.handleMessage(msg);
if(0x123==msg.what){
toastText("連接成功!");
}
}
};
/*連接socket線程*/
{
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
Messagemsg=Message.obtain();
try{
if(null==socket||socket.isClosed()){
socket=newSocket();
socket.connect(newInetSocketAddress(host_ip,host_port),5000);
output=newPrintStream(socket.getOutputStream(),true,
"utf-8");
}
msg.what=0x123;
handler.sendMessage(msg);
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
/*發送信息線程*/
{
Stringmsg;
publicSendThread(Stringmsg){
super();
this.msg=msg;
}
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
try{
output.print(msg);
}catch(Exceptione){
e.printStackTrace();
}
closeSocket();
}
}
{
publicStringtxt1;
publicSocketThread(Stringtxt1){
super();
this.txt1=txt1;
}
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
Messagemsg=Message.obtain();
try{
/*連接伺服器並設置連接超時為5秒*/
if(socket.isClosed()||null==socket){
socket=newSocket();
socket.connect(newInetSocketAddress(host_ip,host_port),5000);
}
//獲取輸入輸出流
PrintStreamou=newPrintStream(socket.getOutputStream(),
true,"utf-8");
BufferedReaderbff=newBufferedReader(newInputStreamReader(
socket.getInputStream()));
//讀取發來伺服器信息
Stringline=null;
buffer="";
while((line=bff.readLine())!=null){
buffer=line+buffer;
}
//向伺服器發送信息
ou.print(txt1);
ou.flush();
//發送消息修改UI線程中的組件
//關閉各種輸入輸出流
bff.close();
ou.close();
socket.close();
msg.what=0x123;
handler.sendMessage(msg);
}catch(UnknownHostExceptione){
handleException(e,"unknownhostexception:"+
e.toString());
}catch(IOExceptione){
handleException(e,"ioexception:"+e.toString());`
}
}
}
privatevoidsendMessage(Stringmsg){
output.print(msg);
}
}
-------------------------------------------------------------------------------------
布局文件:
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/hostIP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="伺服器ip"
android:inputType="text"/>
<EditText
android:id="@+id/hostPort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:inputType="number"
android:hint="埠"/>
<Button
android:id="@+id/btnConnect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Connect"/>
<EditText
android:id="@+id/sendMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="需要發送的內容"
android:inputType="text"/>
<Button
android:id="@+id/btnSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send"/>
</LinearLayout>
㈡ 微信小程序wx.connectSocket連接成功,wx.onSocketMessage打開失敗
if(!handle){MessageBox(_T("綁定埠失敗或者監聽失敗!"));return;}sockaddr from; int fromlen=15;flag=TRUE;while(flag){HANDLE handlefrom=winSocketaccept(handle,&from,&fromlen);//fromlen值小於16就能接收初始化成功。 if(!handlefrom){//MessageBox(_T("啟動接受失敗!"));continue;}BYTE buffer[1024]; int sendlen=0; sendlen=::winSocketRec(handle,buffer,fromlen,MSG_PEEK);//接收語句一直無法接收數據。 if(sendlen<=0){//MessageBox(_T("接受信息失敗!")); int error=0; error=::WSAGetLastError(); // WSAECONNRESETcontinue;}sendlen=::winSocketSend(handle,buffer,fromlen); if(sendlen<=0){//MessageBox(_T("發送信息失敗!"));continue;}}::winSocketClose(handle); HANDLE winSocketbind(const TCHAR* szIp,unsigned short nPort,UINT connet)//根據ip和埠啟動socket連接,最多連接數{LPSOCKET handle=new ENUMSOCKET(); handle->skt=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//連接socket,使用tcp發送字元流 //socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(handle->skt==INVALID_SOCKET)//連接成功{delete handle; return NULL;}//賦值ip值至socket char ip[64]={0}; #ifdef _UNICODE size_t convert=0; ::wcstombs_s(&convert,ip,64,szIp,63);#else_strcpy_s(ip,64,szIp,63);#endifsockaddr_in addr={0}; addr.sin_addr.S_un.S_addr=::inet_addr(ip);//設置ip addr.sin_family=AF_INET; //設置網路服務 addr.sin_port=nPort; //設置埠//建立連接if(bind(handle->skt,(sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR)//連接失敗{delete handle; closesocket(handle->skt); return NULL;}::listen(handle->skt,connet); //連接至網路,返回改socket
望採納!
㈢ 小程序安全域名中,必須是443埠嗎
可以選擇其它自定義埠,但他必須要求HTTPS加密情況下,安裝證書:網頁鏈接
㈣ 小程序的https請求可以改成http請求嗎
小程序的https請求不可以改成http請求。
㈤ 小程序需要伺服器嗎,小程序怎麼配置伺服器
小程序是需要伺服器存放數據的。包括程序本身、資料庫等等內容,都要有個地方存放。
推薦使用最新版的操作系統與環境,微信小程序目前強制HTTPS加密,SSL證書。而且需要備案後才能使用。
伺服器環境配置與搭建可以使用合適的第三方面板來搭建,比如 寶塔面板 7.0 版安裝部署圖文教程 適合小白建站,這裡面提到的面板就符合國人習慣,老魏用起來是感覺很不錯的。再也不用面對黑底白字的命令行了,錯一步也找不到問題出在哪裡,那種痛苦的日子過去了。
安裝好php環境後,上傳小程序到伺服器的網站根目錄,按照小程序部署方法開始按照小程序、外加部署內容。
等到備案通過後,就可以上線了。
㈥ 設計一個小程序, 選擇屏蔽指定的埠
建立個SOCKET,然後注冊該埠不進行任何響應就可以了。
㈦ 哪家小程序開發應用支持多個埠
你說的多個埠是指那種埠,是那種支付、客服、會員、列印之類的埠嗎?像這種埠,基本上都是在開發階段就加進去了的,不過需要你在開發前就提出來的。至於哪家能開發,基本上能夠開發小程序的公司,都能開發的,加幾個埠難度不大,難的是有些埠需要提前申請好的。
㈧ 我想寫個區域網安卓小程序,就是當PC與手機在同一個WLAN內時,PC開放某個埠(1025),
推薦軟體wifi獵手,自動搜索wifi信號並虛擬連接,並且可以手動一鍵破裂密碼(這種方法是不推薦的)
㈨ 微信小程序開發,公眾號只認80埠,小程序又必須要https,怎麼把https的443埠的數據調到80埠上或者
你在逗我?公眾號里掃二維碼進入小程序的。不用考慮什麼埠.......按照小程序的標碼做就好了。
㈩ 微信小程序中我的介面是由ip和埠號組成的怎麼辦
那還不好辦,租VPS啊,固定IP和申請開放相應埠