2个回答
2013-12-15
展开全部
客户端:
#include <iostream>
#include <Winsock2.h>
#include <sstream>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
#define MY_PORT 22222
char g_Buf[1024] = {0};
void main()
{
//网络环境
WSADATA wsaData;
//设置套接字版本号为v2.2
WSAStartup(MAKEWORD(2, 2), &wsaData);
cout << "服务器:开启网络..." << endl;
Sleep(1000);
system("cls");
SOCKET sock; //定义套接字
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //初始化套接字
SOCKADDR_IN ServerAddr = {}; //填充地址信息
ServerAddr.sin_family = AF_INET; //设置协议为TCP协议
ServerAddr.sin_port = /*htons(MY_PORT)*/MY_PORT; //设置端口 必须和服务器端口一致,不然连接不上服务器
cout << "请输入服务器的IP地址:"; //获取访问的地址
char cServerIP[32];
cin >> cServerIP;
ServerAddr.sin_addr.s_addr = inet_addr(cServerIP); //设置访问地址 inet_addr - 将字符串形式的IP地址转换为无符号长整形IP地址
cout << "开始连接服务器..." << endl;
Sleep(1500);
system("cls");
bool Cnt = true;
if (SOCKET_ERROR == connect(sock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr))) //连接服务器
{
cout << "连接服务器失败..." << endl;
Cnt = false;
}
else
{
cout << "成功连接服务器..." << endl;
Cnt = true;
}
while (1) //进入发送信息循环
{
if (Cnt == true)
{
cout << "请输入信息:";
cin >> g_Buf;
int iSend = send(sock, g_Buf, int(strlen(g_Buf) + 1), 0);
if (SOCKET_ERROR == iSend)
{
cout << "发送消息失败..." << endl;
cout << "服务器已经关闭!" << endl;
break;
}
else
{
if (0 == strcmp(g_Buf, "quit"))
{
cout << "发送退出消息..." << endl;
Sleep(1000);
system("cls");
cout << "关闭服务器..." << endl;
break;
}
else
{
Sleep(350);
recv(sock, g_Buf, 1024, 0);
cout << "服务器收到消息:" << g_Buf << endl;
}
}
}
else
{
break;
}
}
//cout << "关闭服务器..." << endl;
Sleep(2000);
system("cls");
cout << "关闭客户端..." << endl;
//释放资源
closesocket(sock);
Sleep(2000);
}
服务器端:
#include<iostream>
#include<Winsock2.h>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
#define MY_PORT 22222 // 端口
char g_Buf[1024] = {0}; //缓冲区
void main()
{
WSADATA wsaData; //网络环境
WSAStartup(MAKEWORD(2, 2), &wsaData); //设置套接字版本号为v2.2
cout << "服务器:开启网络链接..." << endl;
Sleep(1500);
system("cls");
SOCKET sListen; //定义监听套接字
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //初始化监听套接字
SOCKADDR_IN Sadd = {};
Sadd.sin_family = AF_INET; //设置协议为TCP协议
Sadd.sin_port = /*htons(MY_PORT)*/MY_PORT; //设置端口
int SockAddSize = sizeof(SOCKADDR_IN); //套接字数据结构大小
bind(sListen, (SOCKADDR*)&Sadd, SockAddSize); //绑定套接字
cout << "服务器:绑定端口..." << endl;
Sleep(1500);
system("cls");
listen(sListen,SOMAXCONN); //设置套接字为监听模式,设置能同时监听的最大客户数量
cout << "服务器:开始监听..." << endl;
SOCKET sClient; //客户端套接字
SOCKADDR_IN Cadd = {}; //客户端套接字数据,包含IP等总要信息
sClient = accept(sListen, (SOCKADDR*)&Cadd, &SockAddSize); //建立,等待连接
if (INVALID_SOCKET == sClient)
{
WSACleanup();
}
cout<< "服务器:客户端【" <<
(int)Cadd.sin_addr.S_un.S_un_b.s_b1 << "." <<
(int)Cadd.sin_addr.S_un.S_un_b.s_b2 << "." <<
(int)Cadd.sin_addr.S_un.S_un_b.s_b3 << "." <<
(int)Cadd.sin_addr.S_un.S_un_b.s_b4 << "】已连接..." <<endl;
int iRecvNum = 0; //接收字节的个数
while (1)
{
iRecvNum = recv(sClient, g_Buf, 1024, 0); //接收数据
if (iRecvNum < 1)
{
cout << "服务器:客户端失去连接..." << endl;
Sleep(1500);
system("cls");
break;
}
send(sClient, g_Buf, 1024, 0);
if (strcmp(g_Buf,"quit") == 0)
{
cout << "服务器:客户端发送退出命令..." << g_Buf << endl;
break;
}
cout << "客户端" << /*Sadd.sin_port*/MY_PORT << "消息:" << g_Buf << endl;
}
cout << "服务器:准备关闭服务器..." << endl;
closesocket(sListen);
closesocket(sClient);
WSACleanup();
Sleep(2000);
system("cls");
cout << "服务器:已经关闭服务器..." << endl;
Sleep(2000);
}
启动的时候先启动服务器,然后启动客户端
#include <iostream>
#include <Winsock2.h>
#include <sstream>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
#define MY_PORT 22222
char g_Buf[1024] = {0};
void main()
{
//网络环境
WSADATA wsaData;
//设置套接字版本号为v2.2
WSAStartup(MAKEWORD(2, 2), &wsaData);
cout << "服务器:开启网络..." << endl;
Sleep(1000);
system("cls");
SOCKET sock; //定义套接字
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //初始化套接字
SOCKADDR_IN ServerAddr = {}; //填充地址信息
ServerAddr.sin_family = AF_INET; //设置协议为TCP协议
ServerAddr.sin_port = /*htons(MY_PORT)*/MY_PORT; //设置端口 必须和服务器端口一致,不然连接不上服务器
cout << "请输入服务器的IP地址:"; //获取访问的地址
char cServerIP[32];
cin >> cServerIP;
ServerAddr.sin_addr.s_addr = inet_addr(cServerIP); //设置访问地址 inet_addr - 将字符串形式的IP地址转换为无符号长整形IP地址
cout << "开始连接服务器..." << endl;
Sleep(1500);
system("cls");
bool Cnt = true;
if (SOCKET_ERROR == connect(sock, (SOCKADDR*)&ServerAddr, sizeof(ServerAddr))) //连接服务器
{
cout << "连接服务器失败..." << endl;
Cnt = false;
}
else
{
cout << "成功连接服务器..." << endl;
Cnt = true;
}
while (1) //进入发送信息循环
{
if (Cnt == true)
{
cout << "请输入信息:";
cin >> g_Buf;
int iSend = send(sock, g_Buf, int(strlen(g_Buf) + 1), 0);
if (SOCKET_ERROR == iSend)
{
cout << "发送消息失败..." << endl;
cout << "服务器已经关闭!" << endl;
break;
}
else
{
if (0 == strcmp(g_Buf, "quit"))
{
cout << "发送退出消息..." << endl;
Sleep(1000);
system("cls");
cout << "关闭服务器..." << endl;
break;
}
else
{
Sleep(350);
recv(sock, g_Buf, 1024, 0);
cout << "服务器收到消息:" << g_Buf << endl;
}
}
}
else
{
break;
}
}
//cout << "关闭服务器..." << endl;
Sleep(2000);
system("cls");
cout << "关闭客户端..." << endl;
//释放资源
closesocket(sock);
Sleep(2000);
}
服务器端:
#include<iostream>
#include<Winsock2.h>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
#define MY_PORT 22222 // 端口
char g_Buf[1024] = {0}; //缓冲区
void main()
{
WSADATA wsaData; //网络环境
WSAStartup(MAKEWORD(2, 2), &wsaData); //设置套接字版本号为v2.2
cout << "服务器:开启网络链接..." << endl;
Sleep(1500);
system("cls");
SOCKET sListen; //定义监听套接字
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //初始化监听套接字
SOCKADDR_IN Sadd = {};
Sadd.sin_family = AF_INET; //设置协议为TCP协议
Sadd.sin_port = /*htons(MY_PORT)*/MY_PORT; //设置端口
int SockAddSize = sizeof(SOCKADDR_IN); //套接字数据结构大小
bind(sListen, (SOCKADDR*)&Sadd, SockAddSize); //绑定套接字
cout << "服务器:绑定端口..." << endl;
Sleep(1500);
system("cls");
listen(sListen,SOMAXCONN); //设置套接字为监听模式,设置能同时监听的最大客户数量
cout << "服务器:开始监听..." << endl;
SOCKET sClient; //客户端套接字
SOCKADDR_IN Cadd = {}; //客户端套接字数据,包含IP等总要信息
sClient = accept(sListen, (SOCKADDR*)&Cadd, &SockAddSize); //建立,等待连接
if (INVALID_SOCKET == sClient)
{
WSACleanup();
}
cout<< "服务器:客户端【" <<
(int)Cadd.sin_addr.S_un.S_un_b.s_b1 << "." <<
(int)Cadd.sin_addr.S_un.S_un_b.s_b2 << "." <<
(int)Cadd.sin_addr.S_un.S_un_b.s_b3 << "." <<
(int)Cadd.sin_addr.S_un.S_un_b.s_b4 << "】已连接..." <<endl;
int iRecvNum = 0; //接收字节的个数
while (1)
{
iRecvNum = recv(sClient, g_Buf, 1024, 0); //接收数据
if (iRecvNum < 1)
{
cout << "服务器:客户端失去连接..." << endl;
Sleep(1500);
system("cls");
break;
}
send(sClient, g_Buf, 1024, 0);
if (strcmp(g_Buf,"quit") == 0)
{
cout << "服务器:客户端发送退出命令..." << g_Buf << endl;
break;
}
cout << "客户端" << /*Sadd.sin_port*/MY_PORT << "消息:" << g_Buf << endl;
}
cout << "服务器:准备关闭服务器..." << endl;
closesocket(sListen);
closesocket(sClient);
WSACleanup();
Sleep(2000);
system("cls");
cout << "服务器:已经关闭服务器..." << endl;
Sleep(2000);
}
启动的时候先启动服务器,然后启动客户端
2013-12-15
展开全部
现写的程序,调试可以运行:
tcp
服务器端:
#include "iostream"
#include <winsock2.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")//载入win32库
#define MAIN_PORT 5500
#define BUFFER_LEN 1024
void main()
{
WSAData wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);//开启服务
SOCKET ListenSock;
ListenSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//初始化监听套接字
sockaddr_in LocalAddr={0};
LocalAddr.sin_family=AF_INET;
LocalAddr.sin_port=htons(MAIN_PORT);
LocalAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
::bind(ListenSock,(sockaddr*)&LocalAddr,sizeof(sockaddr_in));//将监听套接字绑定到本地一端口上
listen(ListenSock,5);//设置最大监听值
cout<<"开始监听!"<<endl;
tcp
服务器端:
#include "iostream"
#include <winsock2.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")//载入win32库
#define MAIN_PORT 5500
#define BUFFER_LEN 1024
void main()
{
WSAData wsaData;
WSAStartup(MAKEWORD(2,2),&wsaData);//开启服务
SOCKET ListenSock;
ListenSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//初始化监听套接字
sockaddr_in LocalAddr={0};
LocalAddr.sin_family=AF_INET;
LocalAddr.sin_port=htons(MAIN_PORT);
LocalAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
::bind(ListenSock,(sockaddr*)&LocalAddr,sizeof(sockaddr_in));//将监听套接字绑定到本地一端口上
listen(ListenSock,5);//设置最大监听值
cout<<"开始监听!"<<endl;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询