玲玲的专栏

高级web前端开发

java 客户端与c++服务器端tcp通信 demo
java 客户端代码
创建socket
绑定

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;

public class JavaClient {

	public static void main(String [] args) throws InterruptedException, IOException
	{
		InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 5678); //创建socket
		Socket sock = new Socket();  
		sock.connect(addr);//连接服务器
		
		BufferedReader is = null;
		PrintWriter os = null;
	

		os = new PrintWriter(sock.getOutputStream()); //管道的写工具
		is = new BufferedReader(new InputStreamReader(sock.getInputStream()));  //管道的读工具

		BufferedReader user = new BufferedReader(new InputStreamReader(System.in)); //从控制台读取命令的工具
		
		System.out.print("client say:");
		String msg = user.readLine();

		while(!msg .equals("bye"))
		{

			os.println(msg);
			os.flush();
			
			String echo = is.readLine();
			System.out.println(echo);
			
			System.out.print("client say:");
			msg = user.readLine();
		}
	
		os.close();
		is.close();
		sock.close();	
	}	
}

c++服务器端 ,之前的文章写过的,现在拷贝过来的


创建socket

绑定端口

监听端口

// ConsoleServer.cpp : 定义控制台应用程序的入口点。
//

#include <winsock2.h>
#include <process.h>
#pragma comment(lib, "ws2_32.lib")
#include <list>

std::list<SOCKET> client_list;

void thread_client(void * );

int main(int argc, char * argv[])
{
	WSADATA wsadata;
	WSAStartup(0x22, &wsadata);

	unsigned short sPort = argc>1 ? atoi(argv[1]) : 5678;

	int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	
	if (sockfd == INVALID_SOCKET)
	{
		printf("fail to create socket.\n");
		return 1;
	}

	sockaddr_in saddr;
	saddr.sin_addr.s_addr = ADDR_ANY;
	saddr.sin_port = htons(sPort);
	saddr.sin_family = AF_INET;
	if(bind(sockfd, (const sockaddr *)(&saddr), sizeof(saddr)))
	{
		printf("fail to bind, errno: %d.\n", GetLastError());	
		DWORD dw = WSAGetLastError();
		return 2;
	}

	if (listen(sockfd, 5))
	{
		printf("fail to listen.\n");
		return 3; 
	}

	printf("Server is listening at [%d].\n"
		   "Wating for client to connect...\n"
		   , sPort);


	for (;;)
	{
		sockaddr_in caddr;
		int caddr_len = sizeof(caddr);
		int client = accept(sockfd, (sockaddr *)&caddr, &caddr_len);

		unsigned char * pAddr = (unsigned char *)&caddr.sin_addr.s_addr;

		printf("[%d.%d.%d.%d : %d] coming.\n", pAddr[0], pAddr[1], pAddr[2], pAddr[3], ntohs(caddr.sin_port));
		_beginthread(thread_client, 0, (void *)client);
		
		client_list.push_back(client);

	}
	return 0;
}

void thread_client(void * args)
{
	SOCKET client = (int)args;
	sockaddr_in caddr;
	int caddr_len = sizeof(caddr);
	getpeername(client, (sockaddr *)&caddr, &caddr_len);
	unsigned char * pAddr = (unsigned char *)&caddr.sin_addr.s_addr;
	unsigned short usPort = ntohs(caddr.sin_port);

	for (;;)
	{
		char szMsg [0x1000] = "";
		if (0 >= recv(client, szMsg, sizeof(szMsg)-1, 0))
			break;

		char szEcho [0x1000] = "";
		sprintf(szEcho, "Server Say: [%d.%d.%d.%d:%d] %s"
			, pAddr[0], pAddr[1], pAddr[2], pAddr[3]
			, usPort
			, szMsg);

		for (std::list<SOCKET>::iterator it = client_list.begin();
				it != client_list.end();
				it ++)
		{
			send(*it, szEcho, strlen(szEcho), 0);
		}		
	}
}

通信结果显示,在eclipse里面



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linxinyuluo/article/details/7777231
个人分类: 网络通信
想对作者说点什么? 我来说一句

Netty服务端和客户端调用demo

2015年10月04日 67KB 下载

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

不良信息举报

java 客户端与c++服务器端tcp通信 demo

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭