各位走过路过的大哥大姐帮忙看看代码
服务端收发不了数据程序编译可以通过感激不尽。服务端
#include"stdafx.h"
#include<winsock2.h>
#include<windows.h>
#include<memory.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
int sockfd;
int hhh;
char H[200]="aaaAAAA";
SOCKET sockhh;
char buf[200];
WSADATA wsa={0};
WSAStartup(MAKEWORD(2,2),&wsa);
sockfd=socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
my_addr.sin_family=AF_INET;
my_addr.sin_port=1259;
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(my_addr.sin_zero,0,8);
if(bind(sockfd,(LPSOCKADDR)&my_addr,sizeof(my_addr)) == SOCKET_ERROR)
MessageBox(NULL,"jjj",TEXT("返回消息"),MB_OK);
if(listen(sockfd,5) == SOCKET_ERROR)
MessageBox(NULL,"ddd",TEXT("返回消息"),MB_OK);
int addrlen = sizeof(remote_addr);
while(1)
{
sockhh=accept(sockfd,(sockaddr*)&remote_addr,&addrlen);
if(sockhh==INVALID_SOCKET)
MessageBox(NULL,"kkk",TEXT("返回消息"),MB_OK);
//ZeroMemory(buf,sizeof(buf)/sizeof(TCHAR));
//ZeroMemory(H,sizeof(H)/sizeof(TCHAR));
recv(sockhh,buf,sizeof(buf),0);
MessageBox(NULL,buf,TEXT("返回消息"),MB_OK);
send(sockhh,H,sizeof(H),0);
}
closesocket(sockfd);
WSACleanup();
return 0;
}
客户端
#include "stdafx.h"
#include<winsock2.h>
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
SOCKET sockfd;
WSADATA wsa={0};
WSAStartup(MAKEWORD(2,2),&wsa);
sockfd=socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in serv_addr;
struct hostent *host;
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(1259);
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
//serv_addr.sin_zero=8;
connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(struct sockaddr));
TCHAR H[200];
int i=0;
TCHAR buf[]="woaini\0";
ZeroMemory(H,sizeof(H)/sizeof(TCHAR));
recv(sockfd,H,sizeof(H),0);
send(sockfd,buf,sizeof(buf)/sizeof(TCHAR),0);
MessageBox(NULL,H,TEXT("返回消息"),MB_OK);
closesocket(sockfd);
WSACleanup();
return 0;
}
代码写得很烂望包涵。