2

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.

3 Answers 3

1

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,

Sign up to request clarification or add additional context in comments.

1 Comment

@ Anders R. Bystrup Thanx Man
1

Try printing out datavariable, not the return value of InputStreamReader.read in the variable da, like this:

System.out.print(data); 

InputStreamReader.read instead returns the number of characters read.

Comments

0

da is 5 because you read five bytes from the socket. hello consists of 5 bytes, so everything is working just fine. The letters you received have been stored in the variable data.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.