求一个Windows下UDP通信C语言的程序,能编译成功的 5
2015-03-23
展开全部
#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define IOC_VENDOR 0x18000000
#define _WSAIOW(x,y) (IOC_IN|(x)|(y))
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
/*定义聊天记录,用来恢复原来的聊天记录*/
typedef struct ChatRecord{char statement[1024];struct ChatRecord*next;}CR;
/*定义头结点,临时变量*/
CR*head,*T;
/*定义一个临时缓冲区,用来装载被响应函数中断的字符串*/
char strtemp[100]={0};
int i=0;
/*初始化链表*/
void InitRecord(CR*&p)
{
p=(CR*)malloc(sizeof(CR));
p->next=NULL;
T=p;
}
/*添加一项聊天记录*/
void AddRecord(CR*p)
{
T->next=p;
T=p;
p->next=NULL;
}
/*清屏并打印聊天记录*/
void PrintRecord(CR*h)
{
system("CLS");
for(CR*p=h->next;p!=NULL;p=p->next)
{
printf("%s\n",p->statement);
}
printf("input a msg: ");
}
/*定义消息响应函数*/
void UDP_REP(int sockfd)
{
int n;
char msg[1024];
struct sockaddr_in addr;
int addrlen=sizeof(addr);
CR*p;
while(1)
{
n=recvfrom(sockfd,msg,1024,0,(struct sockaddr*)&addr,&addrlen);
fflush(stdin);
if(n==-1){printf("recvfrom() err: %d",WSAGetLastError());exit(1);}
msg[n]=0;
p=(CR*)malloc(sizeof(CR));
strcpy(p->statement,"You say: ");
strcat(p->statement,msg);
AddRecord(p); //添加记录
PrintRecord(head);//打印出记录
if(i!=0)
{
for(int j=0;j<i;j++)printf("%c",strtemp[j]);
}
}
}
/*定义发送函数*/
void UDP_REQ(int sockfd,const struct sockaddr_in*addr,int len)
{
int len1=sizeof(addr);
char buf [1024];
int n=0;
CR*p;
i=0;
printf("input a msg: ");
while(1)
{
fflush(stdin);
//直接从键盘接受字符,不经过输入流,目的是每输入一个字符存入全局变量strtemp,便于恢复清屏前的输入内容
while((buf[i]=strtemp[i]=getch())!='\r')
{
if(buf[i]=='\b')
{
if(i==0)continue;
putchar('\b');
putchar(' ');
putchar('\b');
i--;
continue;
}
else
{
putchar(buf[i]);
}
i++;
}
buf[i]=0;
memset(strtemp,0,100);
i=0;
p=(CR*)malloc(sizeof(CR));
strcpy(p->statement,"I say: ");
strcat(p->statement,buf);
AddRecord(p);
PrintRecord(head);
if(sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)addr,len)==-1)printf("sendto() err: %d",WSAGetLastError());
}
}
/*主函数*/
int main()
{
WSADATA wsaData;
int err;
int port1,port2; //port1为监听端口 port2为发送端口
char Strip[16];
InitRecord(head);
WORD wVersionRequested = MAKEWORD(2,1);
if(err=WSAStartup(wVersionRequested,&wsaData)){printf("init winsock.dll err %d\n",WSAEINVAL) ;exit(1);}
int sockfd1;
struct sockaddr_in addr1,addr2;
sockfd1=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd1<0)
{
printf("sockerr");
system("pause");
exit(1);
}
DWORD dwBytesReturned = 0;
BOOL bNewBehavior = FALSE;
DWORD status;
status = WSAIoctl(sockfd1, SIO_UDP_CONNRESET,
&bNewBehavior,
sizeof (bNewBehavior),
NULL, 0, &dwBytesReturned,
NULL, NULL);
memset(&addr1,0,sizeof(addr1));
memset(&addr2,0,sizeof(addr2));
addr1.sin_family=AF_INET;
addr1.sin_addr.s_addr=htonl(INADDR_ANY);
printf("输入你要监听的端口: ");
scanf("%d",&port1);
printf("输入你要发送的端口: ");
scanf("%d",&port2);
addr1.sin_port=htons(port1);
addr2.sin_family=AF_INET;
printf("输入你要连接的IP: ");
scanf("%s",Strip);
addr2.sin_addr.s_addr=inet_addr(Strip);
addr2.sin_port=htons(port2);
if(bind(sockfd1,(struct sockaddr*)&addr1,sizeof(addr1))<0)
{
printf("bind() err: %d",WSAGetLastError());
system("pause");
exit(1);
}
fflush(stdin);
system("CLS");
CreateThread(0,0,(LPTHREAD_START_ROUTINE)UDP_REP,(void*)sockfd1,0,0); //并发响应和发送两项任务
UDP_REQ(sockfd1,&addr2,sizeof(addr2));
closesocket(sockfd1);
WSACleanup();
return 0;
}
追问
连编出错误了 ,怎么办!unresolved external symbol __imp__WSAGetLastError@0
我想要的是客户端 和服务端两个代码
追答
在代码上加
#pragma comment(lib,"WS2_32.LIB");
引入静态库即可
这个既可以做服务端也可以做服务端 像qq的聊天工具 不是c/s模型 你自己好好研究下吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询