哪位大虾能告诉我socket为什么调用出错,怎么改?
#include <WINSOCK2.H>#pragma comment(lib,"ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <stdarg.h>
int main()
{
int sockfd,BINE,CON,LIN,ACP,SEN,LEN,CLIENT;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);//调用Socket函数//
if(sockfd==-1)
{
perror("Socket错误!\n");//错误提示//
exit(1);
}
my_addr.sin_family=AF_INET;//网络协译TCP/IP//
my_addr.sin_port=htons(3465);//本地端口为2300//
my_addr.sin_addr.s_addr=INADDR_ANY;//自动填入本机IP//
memset(&my_addr,0,sizeof(struct sockaddr_in));
if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(my_addr)==-1))
{
printf("sdfe\n");
exit(1);
}
LIN=listen(sockfd,20);//调用listen()函数监听端口//
if(LIN!=-1)
{
printf("监听端口失败!");//出错提示//
}
CLIENT=sizeof(struct sockaddr);
ACP=accept(sockfd,(struct sockaddr*)&my_addr,&CLIENT);//accept()函数,用于接受客户端的连接请求//
if(ACP==-1)
{
printf("接受请求失败!");
}
char *msg="hello ,socket!";//要发送的内容//
LEN=strlen(msg);//将字符转化为数字//
SEN=send(ACP,msg,LEN,0);//调用send()函数,发送信息//
if(SEN==-1)
{
printf("发送失败!");
closesocket(ACP);//断开连接//
return(0);
}
closesocket(ACP);
}