I am new two android.I want to send data from a android phone (socket client) to my PC (socket server). I connect my android phone to PC via usb cable. I forward the tcp port 8888 to my pc using adb forward command. I have written a UDP server program for my pc and a UDP client program for the android phone. Here is my code
Server:
int main()
{
WSADATA w; /* Used to open windows connection */
unsigned short port_number; /* Port number to use */
/* Components of address in xxx.xxx.xxx.xxx form */
int client_length; /* Length of client struct */
int bytes_received; /* Bytes received from client */
SOCKET sd; /* Socket descriptor of server */
struct sockaddr_in server; /* Information about the server */
struct sockaddr_in client; /* Information about the client */
char buffer[BUFFER_SIZE]; /* Where to store received data */
struct hostent *hp; /* Information about this computer */
char host_name[256]; /* Name of the server */
time_t current_time; /* Current time */
/* Open windows connection */
if (WSAStartup(0x0101, &w) != 0)
{
fprintf(stderr, "Could not open Windows connection.\n");
exit(0);
}
/* Open a datagram socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
exit(0);
}
/* Clear out server struct */
memset((void *)&server, '\0', sizeof(struct sockaddr_in));
/* Set family and port */
server.sin_family = AF_INET;
server.sin_port = htons(8888);
server.sin_addr.s_addr =inet_addr("127.0.0.1");// htonl(INADDR_ANY);
/* Bind address to socket */
if (bind(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Could not bind name to socket.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
printf("\n Server starts running....");
printf("Press CTRL + C to quit\n");
/* Loop and get data from clients */
while (1)
{
client_length = (int)sizeof(struct sockaddr_in);
/* Receive bytes from client */
bytes_received = recvfrom(sd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client, &client_length);
if (bytes_received < 0)
{
fprintf(stderr, "Could not receive datagram.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
printf("\n Revd data from client...");
strcpy(buffer,"\nThis is reply from server....");
/* Check for time request */
/* Get current time */
current_time = time(NULL);
/* Send data back */
if (sendto(sd, (char *)&buffer, (int)sizeof(buffer), 0, (struct sockaddr *)&client, client_length) != (int)sizeof(buffer))
{
fprintf(stderr, "Error sending datagram.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
}
closesocket(sd);
WSACleanup();
return 0;
}
Client:
int main()
{
/* The port number to use */
int sd,byte; /* The socket descriptor */
int server_length; /* Length of server struct */
char send_buffer[1024] = "GET TIME\r\n";/* Data to send */
struct sockaddr_in server; /* Information about the server */
/* Host name of this computer */
char rec_buff[1024];
/* Open a datagram socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd <=0)
{
fprintf(stderr, "Could not create socket.\n");
exit(0);
}
printf("\nSocket Created.....%d",sd);
/* Clear out server struct */
memset((void *)&server, '\0', sizeof(struct sockaddr_in));
/* Set family and port */
server.sin_family = AF_INET;
server.sin_port = htons(8888);
server.sin_addr.s_addr =inet_addr("127.0.0.1");// htonl(INADDR_ANY);
/* Tranmsit data to get time */
server_length = sizeof(struct sockaddr_in);
printf("\n Try to connect with server...%d",server_length);
byte=sendto(sd,(char *)&send_buffer, (int)strlen(send_buffer), 0, (struct sockaddr *)&server, server_length);
printf("\nSend byte ...%d",byte);
if (byte<0)
{
fprintf(stderr, "Error transmitting data.\n");
close(sd);
//exit(0);
}
printf("\n Server connection established....");
/* Receive time */
if (recvfrom(sd, (char *)&rec_buff, (int)sizeof(rec_buff), 0, (struct sockaddr *)&server, &server_length) < 0)
{
fprintf(stderr, "Error receiving data.\n");
close(sd);
exit(0);
}
/* Display time */
printf("Current time: %s", rec_buff);
close(sd);
return 0;
}
Here every time i am getting send byte as zero in the client side. need help..
0 comments:
Post a Comment