m_papas
m_papas

Reputation: 91

send messages from c++ server to java client

I am new in c++ and socket programming. I managed to create a server using c++ in a unix machine and I connected that server to a java client running in a different windows machine. I also managed to send messages from client to server succefully. What I havent managed yet is to send messages from server to client. I suppose it is that I am new to c++ but i didnt find something very helpfull on the internet. I only found c++ client to server communication code and not server to client. Can somebody give some guidance??

Thnx in advance for your time and your help...

C++ server code

int main( int argc, const char** argv )
{


    /* SOCKET VARIABLES */
    int sock;
    struct sockaddr_in server;
    int mysock;
    char buff[1024];
    int rval;


    /*CREATE SOCKET*/
    sock =socket(AF_INET, SOCK_STREAM, 0);

    if (sock<0) 
    {
        perror("*FAILED TO CREATE SOCKET*");
        exit(1);
    }

    server.sin_family=AF_INET;
    server.sin_addr.s_addr=INADDR_ANY;
    server.sin_port=htons(5000);

    /*CALL BIND*/
    if (bind(sock, (struct sockaddr *)&server, sizeof(server)))
    {
        perror("BIND FAILED");
        exit(1);
    }


    /*LISTEN*/
    listen(sock, 5);


    /*ACCEPT*/
    do{

        mysock= accept(sock, (struct sockaddr *) 0, 0);

        if (mysock==-1) 
        {

            perror ("ACCEPT FAILED");
        }
        else
        {
            memset(buff, 0, sizeof(buff));

            if ((rval=recv(mysock, buff, sizeof(buff), 0)) < 0) 
                perror("READING STREAM MESSAGE ERROR");
            else if(rval==0)
                printf("Ending connection");
           else
                printf("MSG: %s\n", buff);

           printf("GOT THE MESSAGE (rval = %d)\n", rval);

            }

    }while (1) ;
    return 0; 

Java client code

public class SOK_1_CLIENT {




    public void run() throws Exception
    {
        String adrress="172.16.151.237"; //localhost -- samemachine
        Socket SOCK =new Socket (adrress,5000);
        PrintStream PS =new PrintStream(SOCK.getOutputStream());
        PS.println("HELLO TO SERVER FROM CLIENT");

        InputStreamReader IR =new InputStreamReader(SOCK.getInputStream());
        BufferedReader BR = new BufferedReader(IR);

        String MESSAGE =BR.readLine();
        System.out.println(MESSAGE + "java");
    }


}

Upvotes: 0

Views: 2547

Answers (3)

bibek shrestha
bibek shrestha

Reputation: 448

Ok I see that you have managed to receive messsage in C++ server and send from java client. See "receiving from socket" in java and "sending from socket" in C++.Use them in the above code and remember when server is sending, at the very time java client should be expecting data from server. That will solve your problem.

If you want to make the server-client like chat application, you have two possibilities:

  • Use "select"
    • Use "threads"

google the keywords highlighted as per your requirement and language used.

see this for sockets in C++ and this for java sockets

Upvotes: 1

Gigi
Gigi

Reputation: 29501

Use send() with the last parameter (flags) set to 0.

If you're on Windows then you may also need to initialise and cleanup Winsock - check this.

Upvotes: 0

Sergey L.
Sergey L.

Reputation: 22552

In C sockets are two-way communisation file descriptors. You can write to them using send(2). Check man 2 send.

 char * buffer;
 size_t length, sent;

 while (length && (sent = send(mysock, buffer, length, 0)) > 0) {
      length -= send;
 }
 if (sent < 0) 
     perror("send");

Please note that unlike with java's BufferedWriter in C send will not guarantee to send the entire buffer in one go. Thus the while loop. If you want buffered socket IO then you can use fdopen to create a stdio stream, but this is not recommended if you wish to be able to pass flags and intercept socket errors.

Upvotes: 1

Related Questions