Member
Registered: Aug 2012
Posts: 94
Original Poster
Rep:
|
Dear Piyush,
Thanks for your reply
Server Program.
void handlesocket(int newsockfd)
{
char *buffer, *temp, *temp2;
int n,i;
if (newsockfd < 0)
error("ERROR on accept");
buffer = new char[960000];
temp = new char[16];
temp[0] = '\0';
temp2 = new char[1];
temp2[0] = '\0';
i = 0;
do
{
n = read(newsockfd,temp2, 1);
if(temp2[0]=='@') break;
temp[i++] = temp2[0];
temp[i] = '\0';
}
while(n > 0);
printf("Waiting for client -----> %s bytes\n",temp);
n = 0;
do
{
buffer[0] = '\0';
//printf("hello");
n += read(newsockfd,buffer,atoi(temp));
buffer[n] = '\0';
writeintofile(buffer);
printf("-----------------\n%s-------------\n",buffer);
}
while(n < atoi(temp));
printf("Recieved ------------>%d bytes\n", n);
n = write(newsockfd,&temp,strlen(temp)-1);
printf("Sent back ------------>%s \n", temp);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
free(buffer);
free(temp);
free(temp2);
}
client program
void sendline(char* data)
{
//char *server_name= "23.23.84.118";
unsigned short port = atoi(portasstring);
int retval, loopflag = 0;
int i, loopcount, maxloop=-1;
unsigned int addr;
int socket_type = DEFAULT_PROTO;
struct sockaddr_in server;
struct hostent *hp;
char *temp;
char *temp1=new char[48000];
sprintf(temp1,"%s%s",formatasstring,data);
sprintf(data,"%s",temp1);
WSADATA wsaData;
SOCKET conn_socket;
if ((retval = WSAStartup(0x202, &wsaData)) != 0)
{
fprintf(stderr,"Client: WSAStartup() failed with error %d\n", retval);
WSACleanup();
}
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
memset(&server, 0, sizeof(server));
memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);
conn_socket = socket(AF_INET, socket_type, 0); /* Open a socket */
if (conn_socket <0 )
{
WSACleanup();
return;
}
if (connect(conn_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
WSACleanup();
return;
}
temp = new char[255];
temp[0] = '\0';
sprintf(temp, "%d@\0", strlen(data));
retval = send(conn_socket, temp, strlen(temp) , 0);
printf("%s\n\n\n\n\n\n\n\n\n\n",temp);
if (retval != SOCKET_ERROR)
{
}
retval = send(conn_socket, data, strlen(data) , 0);
//printf("%s",data);
/*if (retval != SOCKET_ERROR)
{
temp[0] = '\0';
retval = recv(conn_socket, temp, 255, 0);
temp[retval+1] = '\0';
printf("Server Received:%s\n",temp);
}*/
free(temp);
free(temp1);
closesocket(conn_socket);
WSACleanup();
}
|