这是我的C++客户端的代码,但连接不上JAVA的服务端,报错10061,是还需要写些什么吗? WSADATA WsaData;
char buff[BUFFER_SIZE];
memset(buff, 0, sizeof(buff));
if (WSAStartup(MAKEWORD(1, 1), &WsaData) != 0)
{
printf("%d \n", GetLastError());
return;
}
if (LOBYTE(WsaData.wVersion) != 1 || HIBYTE(WsaData.wVersion) != 1)
{
printf("%d \n", GetLastError());
WSACleanup();
system("pause");
return;
}
SOCKET SocketClient = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN root;
root.sin_addr.S_un.S_addr = inet_addr("192.168.0.171");
root.sin_family = AF_INET;
root.sin_port = htons(14870);
if (connect(SocketClient, (struct sockaddr*)&root, sizeof(root)) == INVALID_SOCKET)
{
printf("连接服务器失败: %d \n", WSAGetLastError());
return ;
}
else
{
//接收数据
recv(SocketClient, buff, sizeof(buff), 0);
}
char Buffer[MAX_BUF_LEN];
strcpy(Buffer, Log.c_str());
send(SocketClient, Buffer, sizeof(Buffer), 0);
closesocket(SocketClient);
WSACleanup();本人不懂JAVA语言,不清楚JAVA是这么写的,像我这样可以正常通信吗?这是JAVA服务器的代码public class Receive {
private static final int LISTEN_PORT = 14870;
private ServerSocket serverSocket;
@Test
public void receive(){
System.out.println("日志代理接收器开始启动,监听端口:" + LISTEN_PORT + " 。。。");
try {
serverSocket = new ServerSocket(LISTEN_PORT);
} catch (IOException e) {
System.out.println("启动日志代理接收器失败");
System.out.println(e);
return;
}
while (true){
try(Socket socket = serverSocket.accept()) {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg = br.readLine();
System.out.println("接收到一条日志:"+msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}更新下:现在是我在占用服务器的接受端口14870,但服务器接受不到数据,我的程序也无法往下走
3 回答
慕虎7371278
TA贡献1802条经验 获得超4个赞
JAVA代码中并没有发送数据的书写,而C代码中有一条,导致代码接受不到数据一直在等待,将接受数据删除即可
SOCKET SocketClient = socket(AF_INET, SOCK_STREAM, 0); SOCKADDR_IN root; root.sin_addr.S_un.S_addr = inet_addr("192.168.0.171"); root.sin_family = AF_INET; root.sin_port = htons(14870); if (connect(SocketClient, (struct sockaddr*)&root, sizeof(root)) == INVALID_SOCKET) { printf("连接服务器失败: %d \n", WSAGetLastError()); return ; } char Buffer[MAX_BUF_LEN]; strcpy(Buffer, Log.c_str()); send(SocketClient, Buffer, sizeof(Buffer), 0); closesocket(SocketClient); WSACleanup();
至于前面10061报错是因为没能读取到配置文件
添加回答
举报
0/150
提交
取消