TCP中客户端的建立出现问题,无法建立,在Vcode中报错
#include "cross_host_transmission.h"struct user_info{
char account[400];/*账号*/
char passwd[8];/*密码*/
};
int *p_fd_socket = 0;
#if ON_OFF
void *recv_server_data(void *arg)
{
int result = 0;
char client_recv_data[1024]= {0};
while (1)
{
bzero(client_recv_data,sizeof(client_recv_data));
/*重点:客户端的文件描述符获取客户端的数据*/
result = recv(*p_fd_socket, client_recv_data, sizeof(client_recv_data), 0);
switch(result)
{
case -1:
printf("%s:%d:获取一个客户端信息失败\n", __FUNCTION__, __LINE__);
return ERR_RECV_FAILURE;
break;
case 0:
printf("%s:%d:没有可接收的消息,并且客户端已经执行了有序关闭\n", __FUNCTION__, __LINE__);
return RECV_SUCCESS;
break;
default:
printf("%s:%d:以字节为单位返回消息的长度(%dbytes)\n", __FUNCTION__, __LINE__, result);
return RECV_BYTES;
break;
}
printf("服务器的信息:%s\n",client_recv_data);
if(strcmp("quit",client_recv_data)==0)
{
break;
}
pthread_exit(NULL);
}
#endif
int create_socket(int *p_fd_socket)
{
*p_fd_socket = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == (*p_fd_socket))
{
printf("创建套接字文件失败\n");
return -1;
}
else
{
printf("%s:%d:手机购买成功,手机型号(套接字文件描述符)是:%d\n", __FUNCTION__, __LINE__, *p_fd_socket);
}
}
int bind_ipv4(int socket, uint16_t hostshort, const char *cp)
{
struct sockaddr_in own_addr;
own_addr.sin_family = AF_INET;/*地址簇 IPV4*/
own_addr.sin_port = htons(CLIENT_PORT);/*将端口号转化为网络端口号*/
own_addr.sin_addr.s_addr = inet_addr(CLIENT_IP);/*将IP地址转化为网络IP地址*/
int result = 0;
result = bind(*p_fd_socket, (struct sockaddr *)&own_addr, sizeof(struct sockaddr_in));
if(0 != result)
{
printf("绑定失败\n");
exit(EXIT_FAILURE);
}
else
{
printf("%s:%d:手机号码绑定手机成功,手机号(IP地址)是:%s;手机卡型号(端口号):%d\n",\
__FUNCTION__, __LINE__, CLIENT_IP, CLIENT_PORT);
}
return 0;
}
void client_connect(int socket)
{
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;/*地址簇 IPV4*/
server_addr.sin_port = htons(SERVER_PORT);/*将端口号转化为网络端口号*/
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);/*将IP地址转化为网络IP地址*/
int result = 0;
result = connect(*p_fd_socket, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in));
if(-1 == result)
{
printf("连接服务器失败\n");
return -1;
}
else
{
printf("%s:%d:连接服务器(IP:%s)成功\n", __FUNCTION__, __LINE__, SERVER_IP);
}
#if ON_OFF
/*设计一个线程函数让服务器发送数据给客户端*/
pthread_t server_pthread_id;
pthread_create(&server_pthread_id,NULL,recv_server_data,NULL);
#endif
return 0;
}
/*************************************************
Function: server_recv
Description: 服务器接收一个客户端的信息
Input:
int client_socket:
void *client_data:
size_t client_data_length:
Output:
ERR_RECV_FAILURE:表示-1,获取一个客户端信息失败。
RECV_SUCCESS:表示0,没有可接收的消息,并且客户端已经执行了有序关闭。
RECV_BYTES:表示1,以字节为单位返回消息的长度。
Return: // 函数返回值的说明
Others: // 其它说明
*************************************************/
int client_send(int socket, const void *send_server_data, size_t send_server_data_length)
{
struct user_info user1;
while(1)
{
int result = 0 ;
bzero(&user1, sizeof(struct user_info));
fprintf(stderr, "发送的数据信息:");
scanf("%s%s", user1.account, user1.passwd);
result = send(*p_fd_socket, &user1, sizeof(struct user_info), 0);
if(-1 == result)
{
printf("发送失败\n");
exit(EXIT_FAILURE);
}
else
{
printf("%s:%d:发送%dbytes给手机号(%s)成功\n", __FUNCTION__, __LINE__, result, SERVER_IP);
}
}
#if ON_OFF
printf("%s:%d:客户端主线程工作结束,正在回收子线程\n", __FUNCTION__, __LINE__);
pthread_join(server_pthread_id, NULL);/*阻塞回收线程的资源*/
#endif
/*7.2.8关闭客户端close*/
close(*p_fd_socket);
return 0;
}