软件:VS 2017;
用VS是因为要写界面)
这里用vs会提醒pthread有问题,网上有一个很好的解决方案:https://blog.youkuaiyun.com/cry1994/article/details/79115394
实现:服务器;
先设置全局变量:sockConn是套接字的数组,两个String用来存储注册的用户名和对应的密码

函数如下:
nul_index是查找一个String里面的第一个为空的位置,方便以后注册

///建立与服务器端的连接
int id1 = (unsigned int)pthread_getw32threadid_np(pthread_self()) / 2;
SOCKET sockConn1 = *((SOCKET*)args);//建立套接字
memset(recvBuf, 0, sizeof(recvBuf));
if (::recv(sockConn1, recvBuf, sizeof(recvBuf), 0)<0)
cout << "接受失败\n";
//cout<<recvBuf<<endl;
///这里与客户端呼应,如果客户端传来1就是登陆,如果传来2就是注册;客户端会放在稍后的讲解里面说明。
if (strcmp(recvBuf, "1") == 0)//客户端选择登录
{
cout << "选择登录服务\n";
memset(users, 0, sizeof(users));
if (recv(sockConn1, users, sizeof(users), 0)<0)//接受用户名
cout << "recv failed";
int index = find(user, users);//判断能否找到用户名
if (index != -1)//找到用户名
{
cout << "找到用户名" << endl;
memset(pws, 0, sizeof(pws));
if (recv(sockConn1, pws, sizeof(pws), 0)<0)//接受密码
cout << "recv failed";
if (pws == pw[index])//密码正确
{
success = true;
if (send(sockConn1, "密码正确", sizeof(recvBuf), 0)<0)
cout << "用户登录服务器反应失败";
}
else//密码错误
{
send(sockConn1, "密码错误", sizeof(recvBuf), 0);
}
}
if (index == -1)//未找到用户名
{
memset(pws, 0, sizeof(pws));
if (recv(sockConn1, pws, sizeof(pws), 0)<0)//接受密码
cout << "recv failed";
if (send(sockConn1, "用户名不存在", sizeof(recvBuf), 0)<0)
cout << "用户不存在发送失败";
}
if (success)
{
while (true)
{
memset(recvBuf, 0, sizeof(recvBuf));
int nRecv = ::recv(sockConn1, recvBuf, sizeof(recvBuf), 0);
cout << endl;
if (nRecv>0)//实现转发给其他客户端
{
cout << id1 << "号客户说:" << recvBuf << endl;
if (sockConn1 == sockConn[0])
send(sockConn[1], recvBuf , sizeof(recvBuf), 0);
else
send(sockConn[0], recvBuf , sizeof(recvBuf), 0);
}
else
break;
}
}
}
//cout << recvBuf << endl; 注册代码没考虑密码为空的情况,后继有空再加
if (strcmp(recvBuf, "2") == 0) //客户端选择注册
{
cout << "选择注册服务"<<endl;
memset(users, 0, sizeof(users));
if (recv(sockConn1, users, sizeof(users), 0) < 0)//接收用户名
cout << "register failed"<<endl;
int index = find(user, users);//看看是否已存在用户名
//cout << index <<users << endl;
if (index != -1)//find
{
cout << "用户名已存在" << endl;
if (send(sockConn1, "用户名已存在", sizeof(recvBuf), 0) < 0)
{
cout << "用户注册失败" << endl;
}
}
if (index == -1)//register
{
cout << "用户名未注册过" << endl;
memset(pws, 0, sizeof(pws));
if (recv(sockConn1, pws, sizeof(pws), 0) < 0)//接收密码
cout << "failed pws" << endl;
else//接收密码 添加用户和密码
{
cout << "已经接收到密码" << pws << endl;
int location = nul_index(user);//寻找可以插入的空位
cout << "找到空位在1+" << location << endl;
if (location == nl)//插入已满
{
cout << "注册已满" << endl;
if (send(sockConn1, "注册已满", sizeof(recvBuf), 0) < 0)//发给客户已满信息
cout << "send failed" << endl;
}
else//注册
{
cout << "开始注册:" << endl;
user[location] = users;
pw[location] = pws;
cout << pw[location] << "aaa" <<user[location] << endl;
if (send(sockConn1, "注册成功", sizeof(recvBuf), 0) < 0)//发给客户已满信息
cout <<location+1<<"位用户"<< "注册成功" << endl;
}
}
}
}
return recvBuf;
}
然后是send1:
SOCKET sockClient1 = *((SOCKET*)args);//建立套接字
char buff1[10000];
while (true) {
//cout<<"请输入"<<endl;
cin >> buff1;
if (buff1 == "") {
break;
}
int e = send(sockConn[0], buff1, sizeof(buff1), 0);
if (e == SOCKET_ERROR) {
printf("send failed");
break;
}
/*e = send(sockConn[1], buff1, sizeof(buff1), 0);
if (e == SOCKET_ERROR) {
printf("send failed");
break;
}
cout << "我说:" << buff1 << endl;*/
}
return buff1;
}
sendClient建立连接,好友上线功能还有待上线,该功能会在有好友上线的时候提醒所有在线用户;
使用VS2017进行Socket服务器开发,解决pthread问题,创建服务器并设置全局变量,包括套接字数组和存储用户名、密码的String。通过nul_index找到String的第一个空位置以注册新用户,send1函数用于建立客户端连接,未来计划实现好友上线提醒功能。
5374

被折叠的 条评论
为什么被折叠?



