int connect_to_host(char* server_ip, char* server_port)
{
printf("%s\n", "in connect to host");
printf("server_ip in connect_to_host: %s\n", server_ip);
printf("server_port in connect_to_host: %s\n", server_port);
struct sockaddr_in remote_server_addr;
int fdsocket, port;
uint16_t port = atoi(server_port);
socklen_t len = sizeof(remote_server_addr);
fdsocket = socket(AF_INET, SOCK_STREAM, 0);
if(fdsocket < 0) {
printf("%s\n", "socket error");
return -1;
}
memset(&remote_server_addr, 0, sizeof(remote_server_addr));
remote_server_addr.sin_family = AF_INET;
remote_server_addr.sin_addr.s_addr = inet_addr(server_ip);
//inet_pton(AF_INET, server_ip, &remote_server_addr.sin_addr);
remote_server_addr.sin_port = htons(port);
printf("remote server port after htons: %d\n", remote_server_addr.sin_port);
if(connect(fdsocket, (struct sockaddr*)&remote_server_addr, &len) < 0) {
printf("%s\n", "connect error");
return -1;
}
return fdsocket;
}
此块的输出:
in connect to host
server_ip in connect_to_host: 192.168.1.179
server_port in connect_to_host: 49200
remote server port after htons: 12480
connect error
[LOGIN:ERROR]
[LOGIN:END]
Illegal instruction: 4
服务器初始化为端口49200,其IP与客户端的IP相同(在本地计算机上测试) - 192.168.1.179。
我无法理解连接失败的原因。因为我在不同的函数中以相同的方式执行了connect(),只是在这种情况下连接是UDP并且它工作正常。是因为我想连接到同一个IP吗?
我对此很陌生。如果您需要更多代码,请告诉我们。
答案 0 :(得分:2)
Inductive R := P | Ii | S | N. Fail Check N: Type.
Parameter rp:> R -> Prop. Check N: Type.
Inductive X: Type := | c {z:P} :> X | b {z:P} {y:Ii} :> X.
Parameter (phy:P) (inf:Ii) (sen:S) (nul:N).
Definition xi (y:R) := match y return (match y with (P|Ii) => y | _ => N end) with
| P => phy
| Ii => inf
| _ => nul end.
Eval hnf in xi S. (* = nul *)
问题出在这里。 if(connect(fdsocket, (struct sockaddr*)&remote_server_addr, &len) < 0) {
printf("%s\n", "connect error");
return -1;
}
的第三个参数是connect()
,而不是指针。它应该是:
addrlen_t
打印您自己的错误消息而不是 if(connect(fdsocket, (struct sockaddr*)&remote_server_addr, len) < 0) {
perror("connect error");
return -1;
}
或errno
或perror()
给您的错误消息完全是浪费时间。不要那样做。每次任何系统调用失败时,您必须打印或记录或返回实际错误。
很明显,“非法指令”问题不能来自此代码,因为您在退出后已经打印了更多消息。