C++用socket 做服务端,Java做客户端 可以通信吗?举例说明一哈,急...
发布网友
发布时间:2022-06-10 20:50
我来回答
共1个回答
热心网友
时间:2024-09-06 12:01
当然可以,一点问题也没有啊,只要通信协议定义好,那是完全没有问题的。
//c++服务端
#include <stdio.h>
#include <Winsock2.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[50];
sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[50];
recv(sockConn,recvBuf,50,0);
printf("%s\n",recvBuf);
closesocket(sockConn);
}
}
//java客户端
public class Test{
public static void main(String[] args) throws Exception{
Socket socket = new Socket("127.0.0.1", 6000);
BufferedInputStream bin = new BufferedInputStream(socket.getInputStream());
byte [] bb = new byte[1024];
int len = bin.read(bb);
System.out.println("接收到数据:" + new String(bb, 0 , len));
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write("hello".getBytes());
bos.flush();
bin.close();
bos.close();
socket.close();
}
}
热心网友
时间:2024-09-06 11:58
当然可以,一点问题也没有啊,只要通信协议定义好,那是完全没有问题的。
//c++服务端
#include <stdio.h>
#include <Winsock2.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
listen(sockSrv,5);
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[50];
sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));
send(sockConn,sendBuf,strlen(sendBuf)+1,0);
char recvBuf[50];
recv(sockConn,recvBuf,50,0);
printf("%s\n",recvBuf);
closesocket(sockConn);
}
}
//java客户端
public class Test{
public static void main(String[] args) throws Exception{
Socket socket = new Socket("127.0.0.1", 6000);
BufferedInputStream bin = new BufferedInputStream(socket.getInputStream());
byte [] bb = new byte[1024];
int len = bin.read(bb);
System.out.println("接收到数据:" + new String(bb, 0 , len));
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write("hello".getBytes());
bos.flush();
bin.close();
bos.close();
socket.close();
}
}