的简单socket编程,(简单1v1,简易服务器v多客户端
本帖最后由 漠水 于 2016-12-20 11:59 编辑好久没有发帖了,来一发,很早之前我就想写个1v1,1v多的聊天工具(1v多服务端如果用MVC或者其他的界面语言,可以通过带入的线程结构体参数直接进行回复到客户端,有兴趣的可以自己试试修改写下),死活搞不定,前段时间无聊,百度自己东拼西凑的可以运行的程序,与诸君共勉。
单人聊天代码:
/******************************************************************************************************/
//更改接收端端口ip(RecvPort )和发送端端口ip (SendPort )即可互联(7000-》6000,6000-》)
//设置窗口隐藏
/******************************************************************************************************/
#include<WINSOCK2.H>
#include<stdio.h>
#include<time.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
//#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"") //-- 对应main函数(隐藏窗口)
#pragma warning(disable:4996)
DWORD WINAPIthread_recv(PVOID pParam);
DWORD WINAPIthread_send(PVOID pParam);
DWORD WINAPIthread_accept(PVOID pParam);
const intSendPort = 7000;
const intRecvPort = 6000;
const char* SendIp = "127.0.0.1";
const char* RecvdIp = "127.0.0.1";
#define BUFFER_SIZE 1024 //注意:此Server端数据接收缓冲区 >= Client端数据发送缓冲区 ,否则造成缓冲区溢出
struct THreadRecv{ SOCKET ser; };
struct THreadSend{ SOCKET ser;HANDLE Handle; char buf; };
struct THreadAccept{ SOCKET ser; };
int main(int argc,char *argv[])
{
//ShowWindow(FindWindow("ConsoleWindowClass",argv),0); //查找窗口隐藏自身 ..
//MessageBox(NULL,TEXT("您选择了NO"),TEXT("NO"),MB_OK);
HANDLE Handle_accept = CreateThread(NULL, 0, thread_accept, NULL, 0, NULL);//创建接收端
HANDLE Handle_send = CreateThread(NULL, 0, thread_send, NULL, 0, NULL); //创建发送端
while(true){
}
printf("结束程序");
return 0;
}
//创建线程连接
DWORD WINAPIthread_accept(PVOID pParam) {
WORD version;
WSADATA data;
version = MAKEWORD(1, 1);
int err;
err = WSAStartup(version, &data);
if (!err)
{
printf("套接字一打开\n");
}
SOCKET ser = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //ip地址INADDR_ANY指接收任意数据
addr.sin_port = htons(RecvPort); //端口6000
bind(ser, (SOCKADDR*)&addr, sizeof(SOCKADDR)); //绑定端口
listen(ser, 10); //最大连接数
printf("开始监听\n");
//绑定开始监听
//SOCKETser;
SOCKADDR_IN client;
int len = sizeof(SOCKADDR);
//THreadAccept* acceptParam = (THreadAccept*)pParam;//获取参数结构体ss
//ser = acceptParam->ser;
THreadRecv * recvParam = new THreadRecv();
while (true)
{
SOCKET connect = accept(ser, (SOCKADDR*)&client, &len);
recvParam ->ser=connect;
//SOCKET connect = accept(ser, (SOCKADDR*)&client, &len);
HANDLE Handle = CreateThread(NULL, 0, thread_recv, recvParam, 0, NULL); //创建接收线程
}
return 0;
}
//创建线程接收消息
DWORD WINAPIthread_recv(PVOID pParam) {
printf("接收消息\n");
//绑定开始监听
SOCKETconnect;
THreadRecv* recvParam = (THreadRecv*)pParam;//获取参数结构体ss
connect = recvParam->ser;
while (true)
{
char buf;
memset(buf,0,sizeof(buf)); //初始化数据接收缓冲区
int length;
//最后一个参数一般设置为0
length = recv(connect, buf, BUFFER_SIZE, 0);
if (length != -1){
printf("对方说:%s\n", buf);
}
//if(buf=="dir") system(buf);
}
return 0;
}
//创建线程发送消息
DWORD WINAPIthread_send(PVOID pParam) {
printf("启动发送线程\n");
int err;
WORD verson;
WSADATA data;
verson = MAKEWORD(1, 1);
err = WSAStartup(verson, &data);
//system("pause");
SOCKET client = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN in;
in.sin_addr.S_un.S_addr = inet_addr(SendIp);
in.sin_family = AF_INET;
in.sin_port = htons(SendPort);
int i = -1;
bool myconn = false;
while (i == -1)//连接失败
{
i = connect(client, (SOCKADDR*)&in, sizeof(SOCKADDR)); //套接字连接成功
if (i == -1) {
printf("socket连接失败,是否重连(y/n)\n");
if (getchar() != 'y') i = 0;
Sleep(1000);
}
else{
printf("连接成功\n");
myconn = true;
i = 0;
}
}
charsendchar = {};
//char* sendchar = {};
//char sendchar;
time_t timep;
time(&timep);
if (myconn == true){
printf("请输入消息\n");
while (true){
fflush(stdin);//清除缓存
scanf("%s", sendchar);//输入字符串。
int sendback = send(client, sendchar, strlen(sendchar) + 1, 0);
if (sendback > 0) {
// printf("消息发送成功,返回值为:%d\n", sendback);
printf("%s",ctime(&timep));
//printf("消息发送成功,返回值为:%d\n", sendback);
}
else{ printf("消息发送失败\n"); }
//system("pause");
}
}
closesocket(client); //关闭套接字连接
printf("消息发送线程关闭\n");
return 0;
}
@拈花小仙 请这位大牛点评!{:10_256:}@无符号整形 不错的分享 学习学习,谢谢分享 有这个方面的书籍吗?非常感兴趣 来到鱼C一天的萌新前来学习!{:9_236:} 有没有UDP的呀
页:
[1]