标签:ret def 数据 end lib ons 服务器端 size 接收
#include <WS2tcpip.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <winsock2.h>
#define MAXLINE 512
#define PORT 7210 //填服务器端口号
#define IP_ADDRESS "127.0.0.1" //填服务器ip
int client(SOCKET s)
{
int ire;
char sendline[MAXLINE]={"test"},recvline[MAXLINE];
//memset(sendline,0,MAXLINE);
memset(recvline,0,MAXLINE);
ire = send(s, sendline, (int)strlen(sendline), 0);
if (ire == SOCKET_ERROR)
{
printf("send failed:%d\n",WSAGetLastError());
return -1;
}
printf("\r\n客服端发送数据:%s\r\n",sendline);
do
{
memset(recvline,0,MAXLINE);
ire = recv(s, recvline,MAXLINE, 0);
if (ire>0)
printf("客服端收到数据:%s \r\n",recvline);
else
{
if(ire==0)
printf("服务器终止!\n");
else
printf("recv failed:%d\n",WSAGetLastError());
}
}while (ire>0);
return 0;
}
int main()
{
int n,i;
int iResult;
WSADATA wsaData;
WORD v=MAKEWORD(2,2);
iResult=WSAStartup(v,&wsaData);
if(iResult!=0)
{
printf("WSAStartup failed: %d\n",iResult);
return -1;
} //套接字初始化
int sockfd,con;
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(IP_ADDRESS);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd< 0) {
printf("socket error:%d\n",sockfd);
return -1;
}
servaddr.sin_port = htons(PORT);
con=connect(sockfd, (struct sockaddr*)&servaddr, sizeof(sockaddr_in)); //连接服务器
client(sockfd); //发送 接收数据
iResult=WSACleanup();
if(iResult!=0)
{
printf("WSACleanup failed: %d\n",iResult);
return -1;
} //套接字释放
return 0;
}
标签:ret def 数据 end lib ons 服务器端 size 接收
原文地址:https://www.cnblogs.com/juicef/p/11555788.html