Socket编程中如何发送结构体,拼接多次接收消息及获取通信双方地址

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/gukesdo/article/details/7277146

客户端:

#include <iostream>
#include <stdio.h>
#include <string>
#include "winsock.h"
#pragma comment(lib, "wsock32")

using namespace std;

#define COMMAND 100

int main()
{
	int ret = 0;

	WSADATA wsadata;
	WORD version = MAKEWORD(2,0);
	ret = WSAStartup(version,&wsadata);

	SOCKET m_hClientSocket;
	m_hClientSocket = socket(AF_INET,SOCK_STREAM,0);

	SOCKADDR_IN m_addr1;
	m_addr1.sin_family = AF_INET;
	m_addr1.sin_addr.S_un.S_addr = inet_addr("192.168.100.57");
	m_addr1.sin_port = htons(10000);
	ret = connect(m_hClientSocket,(LPSOCKADDR)&m_addr1,sizeof(m_addr1));

	char Clisend_msg[] = "我是客户端,请问你是那位?";
	//char Clisend_msg[] = "hello, I am a client, and who are you?";
	int len_send = send(m_hClientSocket,Clisend_msg,sizeof(Clisend_msg),0);

	//利用字符串发送,规定好字符串格式在服务器端接收后进行解析
	/*char send_buf[1024] = "tony  2000  ";
	int len_send = send(m_hClientSocket,send_buf,sizeof(send_buf),0);*/

	//使用结构体转换成字符串发送,在服务器端直接转为结构体
	/*
	char send_buf[1024] = "tony  2000  ";
	memset(send_buf,0,1024);
	struct msg
	{
		int cmd;
		int sendID;
		int recvID;
		string name;
		int number;
	};
	msg msg1;
	msg1.cmd = COMMAND;
	msg1.sendID = 2120100324;
	msg1.recvID = 2120100325;
	msg1.name = "Tony";
	msg1.number = 2000;
	

	//以字符串形式发送,因为TCP/IP是字节流通信
	//memcpy(send_buf,&msg1,sizeof(msg));
	//int len_send = send(m_hClientSocket,send_buf,sizeof(send_buf),0);
	int len_send = send(m_hClientSocket,(char *)&msg1,sizeof(msg),0);
	*/

	closesocket(m_hClientSocket);
	WSACleanup();

	return 0;
}


 

服务器端代码:

 

#include <iostream>
#include <stdio.h>
#include <string>
#include "winsock.h"
#pragma comment(lib, "wsock32")

using namespace std;

int main()
{
	//创建socket编程环境
	int ret = 0;
	WSADATA wsadata;
	WORD version = MAKEWORD(2,0);
	ret = WSAStartup(version,&wsadata);

	//服务器端口创建
	SOCKET m_hServerSocket;
	m_hServerSocket = socket(AF_INET,SOCK_STREAM,0);
	if(INVALID_SOCKET == m_hServerSocket)
	{
		cout << "服务器socket创建失败!" << endl;
		return 0;
	}
	else
	{
		cout << "服务器socket创建成功" << endl;
	}
	cout << endl;

	//服务器端口绑定
	SOCKADDR_IN m_addr;
	m_addr.sin_family = AF_INET;
	m_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
	m_addr.sin_port = htons(10000);
	ret = bind(m_hServerSocket,(LPSOCKADDR)&m_addr,sizeof(m_addr));
	

	//服务器端口开始监听
	ret = listen(m_hServerSocket,20);//第二个参数指定最大申请的连接数

	//每当有一个客户端连接申请,则服务器端创建一个线程对其进行处理,模拟数据库服务器的处理方式
	SOCKET com_Sock;
	SOCKADDR_IN clntaddr;
	int clnlen = sizeof(clntaddr);
	struct msg
	{
		int cmd;
		int sendID;
		int recvID;
		string name;
		int number;
	};
	msg msg2;
	
	while(1)
	{
		int count = 0;
		//com_Sock = accept(m_hServerSocket,NULL,NULL);
		com_Sock = accept(m_hServerSocket,(SOCKADDR *)&clntaddr,&clnlen);
		/*cout << inet_ntoa(clntaddr.sin_addr) << endl;

		getpeername(com_Sock,(SOCKADDR *)&clntaddr,&clnlen);
		cout << inet_ntoa(clntaddr.sin_addr) << endl;*/
		getsockname(com_Sock,(SOCKADDR *)&clntaddr,&clnlen);
		cout << inet_ntoa(clntaddr.sin_addr) << endl;

		char recv_msg[16];
		memset(recv_msg,0,16);
		ret = recv(com_Sock,recv_msg,sizeof(recv_msg),0);
		cout << ret << endl;

		//根据给定的字符串格式进行解析
		/*
		for (int i=0;i<4;i++)
		{
			cout << recv_msg[i];
		}
		cout << endl;
		for (int j=6; j<10; j++)
		{
			cout << recv_msg[j];
		}
		cout << endl;*/

		//直接将接收到的字节流拷贝给相应的结构体即可。
		/*
		memset(&msg2,0,sizeof(msg));
		memcpy(&msg2,recv_msg,sizeof(msg));
		cout << msg2.cmd << endl;
		cout << msg2.sendID << endl;
		cout << msg2.recvID << endl;
		cout << msg2.name << endl;
		cout << msg2.number << endl;
		*/


		//在服务器端把多次接收的信息拼接到一起
		char receive[1000];
		memset(receive,0,sizeof(receive));
		int k = 0;

		while (ret > 0)
		{
			for (int m=0; m<16; m++)
			{
				receive[k] = recv_msg[m];
				k++;
			}

			cout << ret << endl;
			count += ret;
			//这里必须加上字符串结束标识,否则会乱码,虽然这已经不是这个
			//数组的单元了。由字符串性质决定的。虽然接受字符串末尾的结束标识,但是如果分多次接收的时候就不行了			                        recv_msg[ret] = '\0';      
			cout << recv_msg << endl;
			ret = recv(com_Sock,recv_msg,sizeof(recv_msg),0);
		}
		cout << count << endl;
		
		cout << receive << endl;
		cout << strlen(receive) << endl;

		closesocket(com_Sock);
	}

	WSACleanup();

	return 0;
}


展开阅读全文

关于socket编程发送接收问题

08-17

// Server.cpp : Defines the entry point for the console application.rn//这是一个服务端rnrn#include "stdafx.h"rn#include rn#include rnusing namespace std;rn#define port 7473rnrnint main(int argc, char* argv[])rnrn WSAData wd;rn WSAStartup(0x0202,&wd);rnrn int i = 0;rn SOCKET sock = socket(AF_INET,SOCK_STREAM,0);rn sockaddr_in myaddr = AF_INET,0;rn myaddr.sin_port = htons(port);rn i = bind(sock,(sockaddr*)&myaddr,sizeof(myaddr));rn listen(sock,5);rn sockaddr_in addrClient;rn int len = sizeof(sockaddr);rn if(i < 0)rn rn cout << WSAGetLastError() << endl;rn rn while(1)rn rn SOCKET socka = accept(sock,(sockaddr*)&addrClient,&len);rn char msg[1000] = 0;rn cout <<" input:" <> msg;rn send(socka,msg,strlen(msg) + 1,0);rn closesocket(socka);rn rn return 0;rnrn下面的是客户端rn// Client.cpp : Defines the entry point for the console application.rn//rnrn#include "stdafx.h"rn#include rn#include rnusing namespace std;rn#define port 7473rnrnint main(int argc, char* argv[])rnrn WSAData wd;rn WSAStartup(0x0202,&wd);rnrn int i = 0;rn SOCKET sock = socket(AF_INET,SOCK_STREAM,0);rn sockaddr_in myaddr = AF_INET,0;rn myaddr.sin_addr.S_un.S_addr = inet_addr("111.178.111.62");rn myaddr.sin_port = htons(port);rn i = connect(sock,(sockaddr*)&myaddr,sizeof(myaddr));rn char buf[1000] = 0;rn recv(sock,buf,1000,0);rn cout << buf << endl;rn if(i < 0)rn rn cout << WSAGetLastError() << endl;rn rn return 0;rnrnrn已经实现了服务端的不停发送rn请问怎么样才能让客户端不停的接收呢?rn谢谢各位了 论坛

没有更多推荐了,返回首页