can someone please explain to me how read write work in sock programming.

my assignment is to run a server and client. the client ask the server "who are you?" and the server replies with the servers name and date and time.

i cannot post my whole code here as its an ongoing assignment and i dont want anyone from my class to have access to my code before i submit.

server

/* transfer data */
//to print string "who are you?"
		int nread = read ( client_sockfd, buf, SIZE ); 
		write ( 1, buf, nread );

		time ( &t );
		gethostname(hostname, sizeof hostname);
		sprintf ( buf, "%s", asctime ( localtime ( &t ) ) );
		socklen_t len = strlen ( buf ) + 1;
		write ( client_sockfd, buf, len );
		write ( client_sockfd, hostname, strlen ( hostname ) + 1 );
		close ( client_sockfd );

my question is do i have to call write() twice? how do i put both date-time and hostname on buf

client

string s = "Who are you?";
/* transfer data */
		write ( sockfd, s, s.size() ); //--compile error
		nread = read ( sockfd, buf, SIZE );
		write ( 1, buf, nread );
		close ( sockfd );
		exit (0);

there is a compiling error in the client.

invalid conversion from ‘char’ to ‘const void*’
BasicClient.c:39: error: initializing argument 2 of ‘ssize_t write(int, const void*, size_t)’

can someone please help me?

drjay

#include <cstdio>
using namespace std;

void testing(const void *buf){
    puts((char*)buf);
}

int main() {
    char *x = "somebytes";
    testing(x);

    return 0;
}

That works here, does that small program also work for you?

hi friends, i am magesh i need c++ code,i have try to do,how to send data client(c++) to server(c#),please any one help to me.. i need very urgent....

client

string s = "Who are you?";
/* transfer data */
		write ( sockfd, s, s.size() ); //--compile error
		nread = read ( sockfd, buf, SIZE );
		write ( 1, buf, nread );
		close ( sockfd );
		exit (0);

there is a compiling error in the client.

invalid conversion from ‘char’ to ‘const void*’
BasicClient.c:39: error: initializing argument 2 of ‘ssize_t write(int, const void*, size_t)’

use the methods send() and recv() for reading/writting to socket.

In write u are passing a string variable whereas it should be a const void *.
u should have tried

char *s = "Who are you?";
write ( sockfd, (void *)s, strlen(s) );

Edited 7 Years Ago by dkalita: n/a

This article has been dead for over six months. Start a new discussion instead.