• C++ socket


    突然发现博客写的太少了,就把好久之前玩的东西拿来凑个数awa

    客户端

    #include<WINSOCK2.H>
    #include<STDIO.H>
    #include<iostream>
    #include<cstring>
    using namespace std;
    #pragma comment(lib,"ws2_32.lib")
    bool off;
    void Recv(PVOID pt){
    	SOCKET sServer=*(SOCKET*)pt;
    	char buf[256];
    	int len;
    	while((len=recv(sServer,buf,256,0))>0){
    		buf[len]=0x00;
    		printf("Receive:%s
    ",buf);
    	}
    	closesocket(sServer);
    	off=true;
    }
    void Send(PVOID pt){
    	SOCKET sServer=*(SOCKET*)pt;
    	char buf[256];
    	while(1){
    		scanf("%s",buf);
            send(sServer,buf,strlen(buf),0);
    	}
    }
    int main(){
    	WORD sockVersion=MAKEWORD(2,2);
    	WSADATA data;
    	if(WSAStartup(sockVersion,&data)!=0)return 0;
    	SOCKET sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	sockaddr_in serAddr;
    	serAddr.sin_family=AF_INET;
    	serAddr.sin_port=htons(8888);
    	serAddr.sin_addr.S_un.S_addr=inet_addr("192.168.2.18");
    	puts("connecting");
    	while(connect(sServer,(sockaddr*)&serAddr,sizeof(serAddr))==SOCKET_ERROR);
    	puts("connected");
    	_beginthread(Send,0,&sServer);
    	_beginthread(Recv,0,&sServer);
    	while(!off);
    	WSACleanup();
    	return 0;
    }
    

    服务端

    #include<bits/stdc++.h>
    #include<winsock2.h>
    #pragma comment(lib,"ws2_32.lib")
    using namespace std;
    set<SOCKET>lst;
    int ClientSum;
    void Recv(PVOID pt){
    	SOCKET sClient=*(SOCKET*)pt;
    	lst.insert(sClient);
    	printf("A new online user,the number of users online:%d
    ",++ClientSum);
    	char buf[256];
    	int len;
    	while((len=recv(sClient,buf,256,0))>0){
    		buf[len]='';
    		printf("Receive:%s
    ",buf);
    	}
    	printf("One user goes offline,the number of users online:%d
    ",--ClientSum);
    	lst.erase(sClient);
    	closesocket(sClient);
    }
    void Send(PVOID pt){
    	char buf[256];
    	while(1){
    		scanf("%s",buf);
    		for(set<SOCKET>::iterator i=lst.begin();i!=lst.end();i++)
    	        send(*i,buf,strlen(buf),0);
    	}
    }
    int main(int argc,char* argv[]){
    	WORD sockVersion=MAKEWORD(2,2);
    	WSADATA wsaData; 
    	if(WSAStartup(sockVersion,&wsaData)!=0)return 0;
    	SOCKET slisten=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    	if(slisten==INVALID_SOCKET){
    		printf("socket error!");
    		return 0;
    	}
    	sockaddr_in sin;
    	sin.sin_family=AF_INET;
    	sin.sin_port=htons(8888);
    	sin.sin_addr.S_un.S_addr = INADDR_ANY;
    	if(bind(slisten,(LPSOCKADDR)&sin,sizeof(sin))==SOCKET_ERROR)printf("bind error!");
    	if(listen(slisten,5)==SOCKET_ERROR)return printf("listen error!"),0;
    	puts("server start success...");
    	SOCKET sClient;
    	sockaddr_in remoteAddr;
    	int nAddrlen=sizeof(remoteAddr);
    	char revData[255];
    	_beginthread(Send,0,NULL);
    	while(true){
    		sClient=accept(slisten,(SOCKADDR*)&remoteAddr,&nAddrlen);
    		if(sClient==INVALID_SOCKET){
    			printf("accept error!");
    			break;
    		}
    		_beginthread(Recv,0,&sClient);
    	}
    	closesocket(slisten);
    	WSACleanup();
    	return 0;
    } 
    
  • 相关阅读:
    learning java identityHashCode
    learning java 获取环境变量及系统属性
    learning java 获取键盘输入
    learning svn add file execuable
    φ累积失败检测算法(转)
    层次锁(转)
    一致性算法中的节点下限(转)
    Fast Paxos(转)
    Zookeeper的一致性协议:Zab(转)
    FLP impossibility
  • 原文地址:https://www.cnblogs.com/nlKOG/p/10754361.html
Copyright © 2020-2023  润新知