java - Sockets- Client's Send not working -
in below code connection between c++ client , java server established using sockets. notified java server. send() in c++ client returning -1. can't seem grab problem.
struct sockaddr_in serv_addr; s_id = socket (pf_inet,sock_stream,0); if(s_id<0) { printf("s_id error \n"); } else { serv_addr.sin_family=af_inet; serv_addr.sin_port =htons (9090); serv_addr.sin_addr.s_addr = inet_addr ("127.0.0.1"); int c_check = connect (s_id,(struct sockaddr *) &serv_addr, sizeof (struct sockaddr)); if(c_check<0) { printf("b_check error \n"); } else { intval temp(values); char *char=new char[sizeof(temp)]; memcpy (&char, &temp, sizeof(temp)); int tempp; tempp=send(s_id,char,sizeof(temp),0); if(tempp==-1) { printf("nae gya\n"); } } } close(s_id);
the line
memcpy (&char, &temp, sizeof(temp)); should be
memcpy (char, &temp, sizeof(temp)); &char gives address of local variable points allocated memory. want copy temp allocated memory instead.
based on description of problem, i'd guess sizeof(temp) > sizeof(char*) write areas of stack in use other variables, including socket handle s_id gets corrupted.
note leak char @ present should add delete[] char after calling send.
alternatively, avoid allocating char @ if pass address of temp send
intval temp(values); int tempp=send(s_id,&temp,sizeof(temp),0);
Comments
Post a Comment