NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TCP/IP java client, c++ server

Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message

Joined: 24 Mar 2011
Posts: 2

PostPosted: Thu Mar 24, 2011 3:48 pm    Post subject: TCP/IP java client, c++ server Reply with quote

I've been struggling to get my java client to communicate with with my c++ server. I'm using tcp sockets.

Here's how I setup my server.


    //descriptor for listening for new connections
    int listenDesc;
    //socket information
    struct sockaddr_in myAddr;
    //client connection object
    clientConnection newClient;
    int connectionDesc;
    //set iterator to the start of the vector
    it = clients.begin();
    //initialize the connection and player id tracker
    currentSize = 0;

    //create socket
    if ((listenDesc = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("open error on socket");

    //set socket information
    myAddr.sin_family = AF_INET;
    myAddr.sin_addr.s_addr = INADDR_ANY;
    myAddr.sin_port = htons(portNum);
    //bind, exit on error
    if (bind(listenDesc, (struct sockaddr *) &myAddr, sizeof(myAddr)) < 0) {
        perror("bind error");

    //listen for new connections, up to 20 can be queued at a time

here's an excerpt of how I'm writing to the socket in the server


        perror("Server: write error");
        delete cfd;

I've done some testing with a dummy client in c and I can communicate fine with the server.

what I'm trying to send over the socket is essentially a char array
for example "3 2 string 4 "

here's how I'm setting up the java client

 DataInputStream Sinput;                // to read the socker

        DataOutputStream Soutput;        // towrite on the socket

        Socket socket;


                       socket = new Socket(InetAddress.getByName(ip), port);


                        Sinput  = new DataInputStream(socket.getInputStream());

                        Soutput = new DataOutputStream(socket.getOutputStream());

example of how I'm reading from socket in client

String response = new String();
                        //String response = bArray.toString();
                        System.out.print("message recieved\n");

essentially I need to take what the c server sends and get it into a string on the java client what am I doing wrong? I currently print the response variable to screen and it looks like random characters. Also I get a string index out of range error.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo