// Server.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <winsock.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define BUF_MAX_LENTH 1024
#pragma comment(lib, "wsock32.lib")
int main()
{
SOCKET sockInt, sockNew;
int addrLen, recvLen;
struct sockaddr_in serverAddr;
struct sockaddr clientAddr;
char buf[BUF_MAX_LENTH]={0};
WORD sockVersion;
WSADATA wsaData;
sockVersion = MAKEWORD(1,1);
WSAStartup(sockVersion, &wsaData);
//创建SOCK
sockInt = socket(AF_INET, SOCK_STREAM, 0);
if(sockInt == INVALID_SOCKET)
{
printf("socket error!/n");
WSACleanup();
return 0;
}
//梆定SOCK
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(3333);
//serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_addr.s_addr = inet_addr("192.168.1.101");
if(bind(sockInt, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
printf("bind error!/n");
WSACleanup();
return 0;
}
/* 找出指定的端口号并打印出来 */
//addrLen = sizeof(serverAddr);
//if(getsockname(sockInt, (struct sockaddr *)&serverAddr, (int *)&addrLen) == SOCKET_ERROR)
//{
// printf("getsockname error/n");
// WSACleanup();
// return 0;
//}
printf("server ip:%s/n", inet_ntoa(serverAddr.sin_addr));
printf("server port:%d/n", ntohs(serverAddr.sin_port));
//监听连接
if(listen(sockInt, 5) == SOCKET_ERROR)
{
printf("listen error/n");
WSACleanup();
return 0;
}
//接收连接
do
{
addrLen = sizeof(clientAddr);
sockNew = accept(sockInt, (struct sockaddr *)&clientAddr, (int *)&addrLen);
if(sockNew == INVALID_SOCKET)
{
printf("accept.........../n");
}
else
{
//接收数据
do
{
memset(buf, 0, BUF_MAX_LENTH);
recvLen = recv(sockNew, buf, BUF_MAX_LENTH, 0);
if(recvLen == SOCKET_ERROR )
{
WSACleanup();
printf("recv error/n");
return 0;
}else if(recvLen > 0)
{
printf("recv content:%s/n", buf);
}else
{
printf("recv close /n");
}
}while(recvLen != 0);
//关闭SOCK
closesocket(sockNew);
}
}while(TRUE);
WSACleanup();
return 0;
}
// client.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <winsock.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define BUF_MAX_LENTH 1024
#pragma comment(lib, "wsock32.lib")
int main()
{
SOCKET sockInt;
struct sockaddr_in serverAddr;
//struct hostent *hp;
char buf[BUF_MAX_LENTH] = {0};
WORD sockVersion;
WSADATA wsaData;
sockVersion = MAKEWORD(1,1);
WSAStartup(sockVersion, &wsaData);
//创建SOCK
sockInt = socket(AF_INET, SOCK_STREAM, 0);
if(sockInt == INVALID_SOCKET)
{
printf("socket error!/n");
WSACleanup();
return 0;
}
//获取服务器IP和端口
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("192.168.1.101");
serverAddr.sin_port = htons(3333);
//hp = gethostbyname(serverIp);
//if(hp = '/0')
//{
// printf("gethostbyname error/n");
// WSACleanup();
// return 0;
//}
//memcpy((char*)&serverAddr.sin_addr, (char*)hp->h_addr, hp->h_length);
//连接服务
if(connect(sockInt, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
printf("connect error/n");
WSACleanup();
return 0;
}
//发送数据
while(TRUE)
{
printf("Please input:/n");
scanf("%s", &buf);
//输入"bye",则退出
if(strcmp(buf, "bye") == 0)
{
break;
}
if(send(sockInt, buf, sizeof(buf), 0) == SOCKET_ERROR)
{
printf("send error/n");
WSACleanup();
return 0;
}
memset(buf, 0, BUF_MAX_LENTH);
}
//关闭SOCK
closesocket(sockInt);
WSACleanup();
return 0;
}