注册 登录
编程论坛 VFP论坛

setsockopt写法正确吗?

ljb8864 发布于 2023-08-27 14:06, 1084 次点击
DECLARE LONG  setsockopt IN "Ws2_32" long s ,long level,long optname ,long optval ,long optlen  &&"wsock32.dll"
DECLARE LONG  getsockopt IN "Ws2_32" long s ,long level,long optname ,long @optval ,long optlen&&"wsock32.dll"
#define  SOL_SOCKET  65535
#define SO_REUSEADDR 0x0004

setsockopt(Thisform.tcpClient.OBJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR, 1, 1)

C语言使用如下
*!*        int on = 1;
*!*        setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));



为了解决Address in use C:\Windows\SysWOW64\mswinsck.ocx

[此贴子已经被作者于2023-8-28 09:32编辑过]

22 回复
#2
吹水佬2023-08-28 22:02
原型
int setsockopt(
  [in] SOCKET     s,
  [in] int        level,
  [in] int        optname,
  [in] const char *optval,
  [in] int        optlen
);
注意optval是char*类型
#3
ljb88642023-08-29 08:23
以下是引用吹水佬在2023-8-28 22:02:34的发言:

原型
int setsockopt(
   SOCKET     s,
   int        level,
   int        optname,
   const char *optval,
   int        optlen
);
注意optval是char*类型



char*类型 在VFP中怎么写?
#4
ljb88642023-08-29 13:17
//C++代码 dll.h
#ifndef LIB_H
#define LIB_H
extern "C" _declspec(dllexport) char* enciphering(char *ch,int &key);
extern "C" _declspec(dllexport) char* deciphering(char *ch,int &key);
#endif

 
//C++代码 Sercret.cpp
#include <iostream>
#include "dll.h"
using namespace std;
char* enciphering(char *ch,int &key)
{
    char *str=new char[100];
    int i=0,x;
    bool decide=true;
    while(ch[i])
    {
        if(ch[i]>='a'&&ch[i]<='z')
        {
            x = ch[i] + i + key + 1;
            while(decide)
            {
                if(x>'z')
                    x-=26;
                else
                    decide=false;
            }
            decide=true;
            str[i]=x;
        }
        else
            str[i]=ch[i];
        i++;
    }
    return str;
}
char* deciphering(char *ch,int &key)
{
    char *str=new char[100];
    int i=0,x;
    bool decide=true;
    while(ch[i])
    {
        if(ch[i]>='a'&&ch[i]<='z')
        {
            x=ch[i]-i-1-key;
            while(decide)
            {
                if(x<'a')
                    x+=26;
                else
                    decide=false;
            }
            decide=true;
            str[i]=x;
        }
        else
str[i]=ch[i];
        i++;
    }
    return str;
}

 

//编译后dll被VFP调用
Declare String enciphering in s_sercret.dll String @, Integer @
Declare String deciphering in s_sercret.dll String @, Integer @
#5
吹水佬2023-08-31 08:13
以下是引用ljb8864在2023-8-29 08:23:20的发言:

char*类型 在VFP中怎么写?

通常char*类型可看作vfp的字符串类型,如果涉及到unicode可能要在尾部添加0h00(chr(0))。
函数声明时通常是string@,如果只作为输入参数(in)可不用描写“@”。
提供的示例:
C语言使用如下
*!*        int on = 1;
*!*        setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
代码不清楚出处,有些编译器对这种语句描述可能会有警告提示(因强制类型转换不太严谨)。
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
vfp可用BINTOC(on,"4rs")来表达
#6
ljb88642023-08-31 08:30
回复 5楼 吹水佬
DECLARE LONG  setsockopt IN "Ws2_32" long s ,long level,long optname ,string optval ,long optlen  &&"wsock32.dll"
DECLARE LONG  getsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long optlen


optval= REPLICATE(0h00,4)
setsockopt( this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR, bintoc(1,"4rs")  , 1)
?getsockopt(this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR,@optval, 4),optval


这样写?
#7
吹水佬2023-08-31 10:18
optlen都是4
#8
金戈铁马22023-08-31 10:32
学习
#9
ljb88642023-08-31 13:52
以下是引用吹水佬在2023-8-31 10:18:08的发言:

optlen都是4



是不是UDP协议无效果?
optval= REPLICATE(0h00,4)
setsockopt( this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR, bintoc(1,"4rs")  , 4)
?getsockopt(this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR,@optval, 4),optval

返回-1
#10
吹水佬2023-08-31 21:11
回复 9楼 ljb8864
跟着用 WSAGetLastError() 看看
#11
ljb88642023-09-01 10:42
回复 10楼 吹水佬
#define  SOL_SOCKET  65535  && 1也没用
#define SO_REUSEADDR   0x0004
#define SO_SNDBUF       7
#define SO_RCVBUF       8

optval= REPLICATE(0h00,4)
?setsockopt( this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR ,bintoc(1,"4rs")  , 4) &&
? this.obJECT.SocketHandle,WSAGetLastError()
?getsockopt(this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR,@optval, 4),optval
?WSAGetLastError()
?getsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,4),optval

返回:
0
1376,0
-1
0
-1

[此贴子已经被作者于2023-9-1 15:01编辑过]

#12
吹水佬2023-09-01 21:28
if getsockopt(this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR,@optval, 4) == -1
    ? WSAGetLastError()
endif
#13
ljb88642023-09-02 11:31
以下是引用吹水佬在2023-9-1 21:28:42的发言:

if getsockopt(this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR,@optval, 4) == -1
    ? WSAGetLastError()
endif



WSAGetLastError() 返回0
#14
吹水佬2023-09-02 17:08
以下是引用ljb8864在2023-9-2 11:31:46的发言:

WSAGetLastError() 返回0

可能不只是get.....的问题,往前找是不是也有异常的地方。
#15
ljb88642023-09-02 17:32
以下是引用吹水佬在2023-9-2 17:08:28的发言:


可能不只是get.....的问题,往前找是不是也有异常的地方。




DECLARE LONG WSAGetLastError IN "Ws2_32"
DECLARE LONG  setsockopt IN "Ws2_32"  s long,level long,optname long ,optval long,optlen long
DECLARE LONG  getsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long optlen

#define SOL_SOCKET 1
#define SO_REUSEADDR 0x0004
 
#define SO_SNDBUF       7
#define SO_RCVBUF       8

Thisform.tcpServer.LocalPort = 1001
Thisform.tcpServer.Listen

optval= REPLICATE(0h00,4)


iok=getsockopt(this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR,@optval, 4)
?iok,"ok"
if iok== -1
    MESSAGEBOX( WSAGetLastError() )
endif
#16
吹水佬2023-09-02 17:53
getsockopt声明是不是有问题?
原型:
int getsockopt(
  [in]      SOCKET s,
  [in]      int    level,
  [in]      int    optname,
  [out]     char   *optval,
  [in, out] int    *optlen
);
注意:optlen 是in/out(输入/输出)的int*(整型指针)类型
#17
ljb88642023-09-04 09:32
回复 16楼 吹水佬
optlen=4
optval= REPLICATE(0h00,4)
?setsockopt( this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR ,bintoc(1,"4rs")  , @optlen) &&
? this.obJECT.SocketHandle,WSAGetLastError()
?getsockopt(this.obJECT.SocketHandle, SOL_SOCKET, SO_REUSEADDR,@optval, @optlen),optval,WSAGetLastError()

这句子可以
-----------------

但下面的设置接收缓存不行,是不是UDP不能设置
optval= 0h00200000
optlen=LEN(optval)
?setsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,@optlen)
?getsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,@optlen),optval
#18
吹水佬2023-09-04 21:32
回复 17楼 ljb8864
不清楚 DECLARE - DLL 具体情况
要注意的是:不同的参数类型 DECLARE - DLL 声明也不同
#19
ljb88642023-09-05 14:14
以下是引用吹水佬在2023-9-4 21:32:37的发言:

不清楚 DECLARE - DLL 具体情况
要注意的是:不同的参数类型 DECLARE - DLL 声明也不同




DECLARE LONG  setsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long @optlen  &&"wsock32.dll"
DECLARE LONG  getsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long @optlen&&"wsock32.dll"
DECLARE LONG WSAGetLastError IN "Ws2_32"

#define  SOL_SOCKET  65535

#define SO_SNDBUF       7
#define SO_RCVBUF       8

optval=  0h00200000
optlen= len(optval)
?setsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,@optlen)
?getsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,@optlen),optval
#20
吹水佬2023-09-05 22:02
DECLARE LONG  setsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long optlen
DECLARE LONG  getsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long @optlen

setsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,optlen)
getsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,@optlen)
#21
ljb88642023-09-06 10:08
以下是引用吹水佬在2023-9-5 22:02:48的发言:

DECLARE LONG  setsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long optlen
DECLARE LONG  getsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long @optlen

setsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,optlen)
getsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,@optlen)




按你设置没有出错,但设置的0h00200000值,设置不上,
DECLARE LONG  setsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long optlen
DECLARE LONG  getsockopt IN "Ws2_32" long s ,long level,long optname ,string @optval ,long @optlen
optval= 0h00200000 &&0h00200000
optlen=LEN(optval)
?setsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,optlen)

optval= REPLICATE(0h00,4)
optlen=LEN(optval)
?getsockopt(this.obJECT.SocketHandle,SOL_SOCKET,SO_RCVBUF,@optval,@optlen),optval


返回结果
0
0 0h00000000
#22
foxfans2023-09-06 11:34
        *!* #define SOL_SOCKET 0xffff
        *!* #define SO_RCVBUF 0x1002
        *!* DECLARE LONG setsockopt IN "Ws2_32" long,long,long,string,long
        *!* DECLARE LONG getsockopt IN "Ws2_32" long,long,long,string @,long @
        m.optvalSet = BINTOC(4096,"4RS")
        m.optvalGet = REPLICATE(0h00,4)
        m.optvalLen=4
        IF setsockopt(m.hSocket,SOL_SOCKET,SO_RCVBUF,m.optvalSet,m.optvalLen)<>-1  
            IF getsockopt(m.hSocket,SOL_SOCKET,SO_RCVBUF,@m.optvalGet,@m.optvalLen)<>-1
               MESSAGEBOX(CTOBIN(m.optvalGet,"4rs"))
            ENDIF
        ENDIF  
        实际上getsockopt倒数第2个参数可以不用string @,换成long @即可, 这样replica(0h00,4), 直接用 0  

        *!* #define SOL_SOCKET 0xffff
        *!* #define SO_RCVBUF 0x1002
        *!* DECLARE LONG setsockopt IN "Ws2_32" long,long,long,string,long
        *!* DECLARE LONG getsockopt IN "Ws2_32" long,long,long,long @,long @
        m.optvalSet = BINTOC(4096,"4RS")
        m.optvalGet = 0
        m.optvalLen=4
        IF setsockopt(m.hSocket,SOL_SOCKET,SO_RCVBUF,m.optvalSet,m.optvalLen)<>-1  
            IF getsockopt(m.hSocket,SOL_SOCKET,SO_RCVBUF,@m.optvalGet,@m.optvalLen)<>-1
               MESSAGEBOX(m.optvalGet)
            ENDIF
        ENDIF  
只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2023-9-6 11:40编辑过]

#23
ljb88642023-09-06 12:35
回复 22楼 foxfans
UDP协议,这个能这样设置吗?
1