我有一个服务器 - 客户端java代码,服务器部分必须用c编写。 代码的第一部分(服务器端)是从客户端接收多播消息,然后通过tcp连接连接到客户端并发送一些消息。使用我的c代码,我可以从客户端收到多播消息,但是当我尝试建立tcp连接时,我收到错误。 c服务器无法与java客户端建立TCP连接。我发布以下代码。如果您能找到问题,请告诉我。谢谢。 IPMulticastDiscoveryClient.java客户端代码
package org.postgresql.discovery;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import org.postgresql.discovery.ClientTCPResponseProvider;
/**
* A feature for detecting remote service addresses (IPs) via IP Multicast.
*
* Each service must first bind itself with the help of the IPMulticastDiscovery Server
* and is identified with a unique name.
*
*
*
*/
public class IPMulticastDiscoveryClient {
private static String myIp = null;
public IPMulticastDiscoveryClient(){
}
/**
* Locates a bound service with a unique name via IPMulticast
*
* @param service unique name of the service
* @return the IP address of the service as String
* @throws Exception
*/
public String getIpForService(String service, String host) throws Exception{
//CloudCacheLogger.info("Looking for service: " + service);
// 1. open a response channel
ClientTCPResponseProvider response = new ClientTCPResponseProvider(service);
// 2. multicast request
sendJoinCluster(service, host);
// 3. collect response
//if(response.getFinalResult() == null){
//}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String result = response.getFinalResult();
//myIp = result.split(";")[1];
//result = result.split(";")[0];
// 4. close the response channel
response.destroy();
// 5. check
if(result == null){
throw new Exception("Could not discover service: " + service);
} else {
//CloudCacheLogger.info("found service: " + service + " at " + result);
return result;
}
}
/**
* Uses a remote service to discover the local, external IP address
*
* @param service unique name of a remote service
* @return the local, external ip address
* @throws Exception
*/
public String getMyIpFromService(String service) throws Exception{
if(myIp != null)
return myIp;
else throw new Exception("could not determine local ip");
}
private static void sendJoinCluster(String message, String host){
try {
System.out.println("\nIn sendJoinCluster");
InetAddress group = InetAddress.getByName(host);
MulticastSocket s = new MulticastSocket(9876);
s.joinGroup(group);
DatagramPacket hi = new DatagramPacket(message.getBytes(), message.length(),group, 6789);
s.send(hi);
s.leaveGroup(group);
s.close();
} catch (Exception e){
e.printStackTrace();
}
}
public static void main (String args[]) throws Exception {
IPMulticastDiscoveryClient client = new IPMulticastDiscoveryClient();
String result = client.getIpForService("FindIP", "228.5.6.7");
System.out.println(result);
}
}
ClientTCPResponseProvider.java
package org.postgresql.discovery;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* Offers a socket for receiving repsonses from remote services. Once the client
sent a location
* request, it waits for messages to arrive on this Socket.
*
*
*
*/
public class ClientTCPResponseProvider {
private ServerSocket providerSocket;
private Socket connection = null;
private ObjectOutputStream out;
private ObjectInputStream in;
private boolean endConnection = false;
private String keyword = null;
private String finalResult = "";
public ClientTCPResponseProvider(String keyword){
System.out.println("\nIn ClientTCPResponseProvider");
this.keyword = keyword;
Thread t = new Thread(new ThreadedProvider());
t.start();
}
public void destroy(){
endConnection = true;
try {
providerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void goConnect(){
try{
System.out.println("\nIn goConnect.");
//1. creating a server socket
providerSocket = new ServerSocket(8888);
//2. Wait for connection
Object message = null;
long timeout = 1000;
long initTime = System.currentTimeMillis();
do{
//System.out.println("Waiting for connection...");
connection = providerSocket.accept();
//System.out.println("Connection received from " + connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
//4. The two parts communicate via the input and output streams
try{
message = in.readObject();
String response = (String)message;
//System.out.println("received: " + response);
if(response.startsWith(keyword) && response.split(":")[0].equals(keyword)){
finalResult += connection.getInetAddress().getHostAddress()+";";
System.out.println(" IVAN " +finalResult);
//finalResult += connection.getLocalAddress().getHostAddress()+ ";";
//endConnection = true;
}
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
in.close();
out.close();
connection.close();
} while(System.currentTimeMillis()-initTime < timeout);
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
private class ThreadedProvider implements Runnable{
//@Override
public void run() {
goConnect();
}
}
public String getFinalResult() {
return finalResult;
}
}
C中的服务器代码
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <pthread.h>
void *listenToClient();
char *trim(char *str);
void sendMessageToClient(char *ip, int *port, char *message);
#define HELLO_PORT 6789
#define HELLO_GROUP "228.5.6.7"
#define MSGBUFSIZE 256
char *ClientIP = "localhost";
char *HostIP = "192.168.164.138";
int isRunning = 1;
main(int argc, char *argv[])
{
pthread_t thread;
int th;
th = pthread_create( &thread, NULL, listenToClient, NULL);
pthread_join( thread, NULL);
exit(0);
}
void *listenToClient(){
struct sockaddr_in addr;
int fd, nbytes,addrlen;
struct ip_mreq mreq;
char msgbuf[MSGBUFSIZE];
u_int yes=1;
/* create what looks like an ordinary UDP socket */
if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("socket");
exit(1);
}
/* allow multiple sockets to use the same PORT number */
if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
perror("Reusing ADDR failed");
exit(1);
}
/* set up destination address */
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port=htons(HELLO_PORT);
/* bind to receive address */
if (bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
perror("bind");
exit(1);
}
/* use setsockopt() to request that the kernel join a multicast group */
mreq.imr_multiaddr.s_addr=inet_addr(HELLO_GROUP);
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
perror("setsockopt");
exit(1);
}
do {
addrlen=sizeof(addr);
if ((nbytes=recvfrom(fd,msgbuf,MSGBUFSIZE,0,(struct sockaddr *) &addr,&addrlen)) < 0) {
perror("recvfrom");
exit(1);
}
puts(msgbuf);
char message[] = "FindIP:192.168.164.138 (Some IP)\n");
printf("\nSending Message To Client: %s\n", message);
sendMessageToClient(ClientIP, 8888, message);
}while(isRunning==1);
close(fd);
}
/**Sends the messages to the remote machine
* ip = Address of the machine where message has to sent
* port = port used for sending the message
* message= string message
*
*/
void sendMessageToClient(char *ip, int *port, char *message) {
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
portno = port;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(ip);
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
bzero(buffer, 256);
strcpy(buffer,message);
n = send(sockfd, buffer, strlen(buffer), 0);
if (n < 0)
error("ERROR writing to socket");
bzero(buffer, 256);
close(sockfd);
return 0;
}
答案 0 :(得分:0)
portno = port;
我怀疑你想要
portno = *port;
答案 1 :(得分:0)
此功能的声明不正确:
void sendMessageToClient(char *ip, int *port, char *message)
port
的类型应为int
,而不是int *
。无论客户端消息来自何处,代码始终连接到localhost:8888
;那是你的意图吗?
顺便说一句,您在C代码中使用了许多过时的函数(例如bcopy()
,bzero()
和gethostbyname()
)。