从c ++发送tcp数据并接收到java socket

时间:2014-08-12 06:06:23

标签: java c++ serversocket asyncsocket

我正在使用visual c ++创建一个应用程序,它将使用tcp与java服务器通信。我怎么能用套接字编程来做到这一点。

1 个答案:

答案 0 :(得分:2)

所以你想要的只是使用套接字在java中创建服务器,这些套接字将监听你使用c ++套接字创建的客户端并提供将在服务器上运行的服务器地址和端口 。

示例client in C++

#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];

if (argc < 3) {
    fprintf(stderr,"usage %s hostname port\n", argv[0]);
    exit(0);
}
portno = atoi(argv[2]);
/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) 
{
    perror("ERROR opening socket");
    exit(1);
}
server = gethostbyname(argv[1]);
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);

/* Now connect to the server */
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) 
{
     perror("ERROR connecting");
     exit(1);
}   
/* Now ask for a message from the user, this message
* will be read by server
*/
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
/* Send message to the server */
n = write(sockfd,buffer,strlen(buffer));
if (n < 0) 
{
     perror("ERROR writing to socket");
     exit(1);
}
/* Now read server response */
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0) 
{
     perror("ERROR reading from socket");
     exit(1);
}
printf("%s\n",buffer);
return 0;
}

示例server in java

// File Name GreetingServer.java

import java.net.*;
import java.io.*;

public class GreetingServer extends Thread
{
private ServerSocket serverSocket;

public GreetingServer(int port) throws IOException
{
  serverSocket = new ServerSocket(port);
  serverSocket.setSoTimeout(10000);
}

public void run()
{
  while(true)
  {
     try
     {
        System.out.println("Waiting for client on port " +
        serverSocket.getLocalPort() + "...");
        Socket server = serverSocket.accept();
        System.out.println("Just connected to "
              + server.getRemoteSocketAddress());
        DataInputStream in =
              new DataInputStream(server.getInputStream());
        System.out.println(in.readUTF());
        DataOutputStream out =
             new DataOutputStream(server.getOutputStream());
        out.writeUTF("Thank you for connecting to "
          + server.getLocalSocketAddress() + "\nGoodbye!");
        server.close();
     }catch(SocketTimeoutException s)
     {
        System.out.println("Socket timed out!");
        break;
     }catch(IOException e)
     {
        e.printStackTrace();
        break;
     }
  }
}
public static void main(String [] args)
{
  int port = Integer.parseInt(args[0]);
  try
  {
     Thread t = new GreetingServer(port);
     t.start();
  }catch(IOException e)
  {
     e.printStackTrace();
  }
 }
}