Unable to receive data from the server

Sandeep

This is my c server side code. I am sending hello to the client which is on java

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

#define PORT 6865
#define BUF 256

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

int read_t,i;
char data[]="hello";

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)) {
    perror("bind error is\n");
printf("errorno is %d\n",errno);
    return 1;
}

if(listen(host_fd, 10)) {
    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");

//read_t = recv(remote_fd,data,sizeof(data),0);
//data[read_t]='\0';
printf("read = %d, data = %s \n", sizeof(data), data);
if(sendto(remote_fd,data,sizeof(data),0,NULL,1)==-1)
{
    printf("error in sending back\n");
    return 1;
}
    read_t = recv(remote_fd,data,sizeof(data),0);
data[read_t]='\0';
    printf("read = %d, received_data = %s \n", sizeof(data), data);
shutdown(remote_fd, SHUT_RDWR);
close(remote_fd);

return 0;
}  

This is the java code

  package com.java.client;

 import java.io.BufferedInputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.net.Socket;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;

  public class ClientFirst
  {
 public static void main(String[] argv) throws UnknownHostException, IOException 
 {
     char[] data = new char[10];
     Socket s = new Socket("10.9.79.80", 6865);


     InputStreamReader ins= new  InputStreamReader(s.getInputStream());
         int da= ins.read(data, 0, 9);
         System.out.print(Integer.toString(da));

    }
 }

When i am trying to receive the data from c server i am not getting it. Instead of getting hello, i am getting 5.

Anders R. Bystrup

You're printing the number of characters read from the stream, not the actual data read. Try this instead:

int da = ins.read(data, 0, 9);
System.out.print( data );

Cheers,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to receive data from the server

From Dev

C++ server unable to receive data from client / Winsock

From Dev

unable to receive data on server side in socket communication

From Dev

Unable to receive response data from @ExceptionHalndler method

From Dev

Server does not receive data from ajax call

From Dev

Unable to receive data on server side (Java Socket Programming)

From Dev

Unable to receive data with libusb

From Dev

Unable to receive the entire data frame from Sqlite using R (dplyr)

From Dev

Why am I unable to receive data from this website?

From Dev

Driver is unable to receive data from all executors for each partition

From Dev

Unable to receive String Data from Python Socket to Java Socket

From Dev

Java Parsing Issue- How to Receive data from Server?

From Dev

Receive binary data in web browser from HTTP server in a streaming style

From Dev

Is bind() necessary if I want to receive data from a server using UDP?

From Dev

I receive no data from server after multiple POST requests

From Dev

Send and receive data from server using 6.0 API (Android)

From Dev

Android app - problem with send and receive data from server

From Dev

How to receive data from one server to another using PHP

From Dev

Receive data sent from C++ server on C# client

From Dev

Ajax jquery issue when I receive data from the server

From Dev

Ajax Successfully Receive Data From Server, But Failed Sending It

From Dev

how to receive large data from a broadcast server using python sockets

From Dev

error: send data from html page to server and receive some data from server

From Dev

Unable to receive broadcast from Service

From Dev

Unable to parse JSON data from server

From Dev

Unable to fetch JSON data from Server

From Dev

Unable to send data from my server to client

From Dev

Unable to parse JSON data from server

From Dev

Unable to send data from node server to ajax

Related Related

  1. 1

    Unable to receive data from the server

  2. 2

    C++ server unable to receive data from client / Winsock

  3. 3

    unable to receive data on server side in socket communication

  4. 4

    Unable to receive response data from @ExceptionHalndler method

  5. 5

    Server does not receive data from ajax call

  6. 6

    Unable to receive data on server side (Java Socket Programming)

  7. 7

    Unable to receive data with libusb

  8. 8

    Unable to receive the entire data frame from Sqlite using R (dplyr)

  9. 9

    Why am I unable to receive data from this website?

  10. 10

    Driver is unable to receive data from all executors for each partition

  11. 11

    Unable to receive String Data from Python Socket to Java Socket

  12. 12

    Java Parsing Issue- How to Receive data from Server?

  13. 13

    Receive binary data in web browser from HTTP server in a streaming style

  14. 14

    Is bind() necessary if I want to receive data from a server using UDP?

  15. 15

    I receive no data from server after multiple POST requests

  16. 16

    Send and receive data from server using 6.0 API (Android)

  17. 17

    Android app - problem with send and receive data from server

  18. 18

    How to receive data from one server to another using PHP

  19. 19

    Receive data sent from C++ server on C# client

  20. 20

    Ajax jquery issue when I receive data from the server

  21. 21

    Ajax Successfully Receive Data From Server, But Failed Sending It

  22. 22

    how to receive large data from a broadcast server using python sockets

  23. 23

    error: send data from html page to server and receive some data from server

  24. 24

    Unable to receive broadcast from Service

  25. 25

    Unable to parse JSON data from server

  26. 26

    Unable to fetch JSON data from Server

  27. 27

    Unable to send data from my server to client

  28. 28

    Unable to parse JSON data from server

  29. 29

    Unable to send data from node server to ajax

HotTag

Archive