㈠ 求给一个安卓小程序的代码 就是单击按钮然后就与指定端口建立连接
我之前写过一个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和申请开放相应端口