-2

I am creating an application using visual c++ which will communicate with java server using tcp. how can I do this using socket programming.

roalz
  • 2,699
  • 3
  • 25
  • 42
  • 2
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackoverflow.com/questions/156810/stack-overflow-question-checklist). You might also want to learn how to create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Some programmer dude Aug 12 '14 at 06:08

1 Answers1

2

So what you want just create server in java using sockets that will listen to your client which you crate using c++ socket and give the server address and port which will be running on server .

example 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;
}

example 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();
  }
 }
}
smali
  • 4,687
  • 7
  • 38
  • 60
  • if you get any specific problem in this let me know. – smali Aug 12 '14 at 06:23
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – BetaRide Aug 12 '14 at 07:13
  • thanks for making a good point – smali Aug 12 '14 at 07:19