Sandeep
Sandeep

Reputation: 173

In socket programming I am getting error on receiving string on the server side which has code written in c

This is my C server code. I am sending a string from java to c using socket connection.

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

#define PORT 6865
#define BUF 2048

int main(int argc, char *argv[])
{
     struct sockaddr_in host, remote;
    int host_fd, remote_fd;
    int size = sizeof(struct sockaddr);;

    char *data[BUF];

    host.sin_family = AF_INET;
    host.sin_addr.s_addr = htonl(INADDR_ANY);
    host.sin_port = htons(PORT);
    memset(&host.sin_zero, 0, sizeof(host.sin_zero));

    host_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(host_fd == -1) 
    {
        printf("socket error %d\n", host_fd);
        return 1;
    }

    if(bind(host_fd, (struct sockaddr *)&host, size)) 
    {
        printf("bind error\n");
        return 1;
    }

     if(listen(host_fd, 5))
     {
         printf("listen error");
         return 1;
    }

    printf("Server setup, waiting for connection...\n");
    remote_fd = accept(host_fd, (struct sockaddr *)&remote, &size);

    printf("connection made\n");

   int read = recv(remote_fd,&data,BUF, 0);
   // data[read]='\0';
   printf("read = %d, data = %s\n", read, data);
   shutdown(remote_fd, SHUT_RDWR);
   close(remote_fd);


   return 0;
 }

This is the java code I am sending a string to the server.

    package com.java.client;

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;

     public class ClientFirst
    {
    public static void main(String[] argv) 
    {
        DataOutputStream os = null;

        try 
        {
            Socket socket = new Socket("10.9.79.80", 6865);
            os = new DataOutputStream(socket.getOutputStream());
            //String ch="shanx";
            os.writeUTF("harsh");


           /*char ch;
           in=new DataInputStream(socket.getInputStream());
            System.out.println(""+in.readChar());*/

            os.close();
            socket.close();

        } catch (UnknownHostException e) {
            System.out.println("Unkonw exception " + e.getMessage());

        } catch (IOException e) {
            System.out.println("IOException caught " + e.getMessage());
        } 
    }

}

The c code is displaying only 2,3 characters and some junk data also like unknown symbols from the string. Please help me with this

Upvotes: 0

Views: 161

Answers (3)

UBUNTU
UBUNTU

Reputation: 1

 int read = recv(remote_fd,&data,BUF, 0);

instead of using "&data" you should use (void *)&data. And also you should change char *data[BUFF] TO char data[BUFF].

Upvotes: 0

Gyro Gearless
Gyro Gearless

Reputation: 5279

I guess the problem is in your C code - you need to declare char data[BUF] (in your example, your declare an array of 2048 char*'s ). Should work then.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503629

DataOutputStream.writeUTF doesn't just write the characters out in a way that C is going to understand:

  • It uses a modified version of UTF-8
  • It prepends the length of the string

You could write code in C to handle that, but you'd need to do so very explicitly. (You may find that someone else has already done that.)

Alternatively, you could use an OutputStreamWriter and write the data out in whatever normal encoding you want, potentially using some other scheme to specify the length of the string. You need to work out your requirements first though - do you need to handle non-ASCII text? Are you going to send multiple strings over the same socket?

Also note that on the C side, you should perform appropriate 0-termination after the data that you've read, in order to handle it as a normal "C string".

Upvotes: 3

Related Questions