1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
char buf1[] = {0xff, 0xfb, 0x18, 0xff, 0xfb, 0x1f};
char buf2[] = {0xff, 0xfc, 0x20, 0xff, 0xfc, 0x23, 0xff, 0xfb, 0x27};
char buf3[] = {0xff, 0xfa, 0x1f, 0x00, 0x78, 0x00, 0x32, 0xff, 0xf0};
char buf4[] = {0xff, 0xfa, 0x27, 0x00, 0xff, 0xf0, 0xff, 0xfa, 0x18, 0x00, 0x41, 0x4e, 0x53, 0x49, 0xff, 0xf0};
char buf5[] = {0xff, 0xfd, 0x03};
char buf6[] = {0xff, 0xfb, 0x01, 0xff, 0xfe, 0x05, 0xff, 0xfc, 0x21};
char buf7[] = {0xff, 0xfc, 0x01};
char buf8[] = {0xff, 0xfd, 0x01};
void read (int sock)
{
char buffer[256];
/* Now read server response */
memset(buffer, 0, sizeof(buffer));
int n = recv( sock, buffer, 255, 0 );
if (n < 0)
{
perror("ERROR reading from socket");
return;
}
printf("%d bytes received buffer is: %s\n", n, buffer);
for (int i = 0; i < n; i++)
printf("%2x ", buffer[i]);
printf("\n");
}
void mwrite (int sock, char * buf, int size)
{
int n = send( sock, buf, size, 0 );
if (n < 0)
{
perror("ERROR writing to socket");
return;
}
printf("Bytes Sent: %d\n", n);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
return(0);
}
portno = atoi(argv[2]);
/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
return(1);
}
server = gethostbyname(argv[1]);
if (server == NULL)
{fprintf(stderr,"ERROR no such host \n");
exit(0);}
//printf("host %s, port %d\n", host.c_str(), portno);
bzero((char *) &serv_addr , sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char*)&serv_addr.sin_addr.s_addr, server->h_length);
//serv_addr.sin_addr.s_addr = inet_addr( host.c_str() ); // ( "127.0.0.1" );
serv_addr.sin_port = htons( portno );
/* Now connect to the server */
if (connect( sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr) ) < 0)
{
perror("ERROR connecting");
return(1);
}
printf("Please enter the message=");
bzero(buffer,256);
fgets(buffer,255,stdin);
n= write(sockfd,buffer,strlen(buffer));
if(n<0)
printf("ERROR writing in socket %d len %d", n, strlen(buffer));
bzero(buffer,256);
n = read(sockfd, buffer, 255);
if(n<0)
perror("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
return 0;
buffer[0] = 0x0d;
buffer[1] = 0x0a;
mwrite ( sockfd, buffer, 2);
printf("read 1 ");
read(sockfd);
mwrite( sockfd, buf1, sizeof(buf1));
sleep(2);
mwrite( sockfd, buf2, sizeof(buf2));
printf("read 2 ");
read(sockfd);
mwrite( sockfd, buf3, sizeof(buf3));
printf("read 3a ");
read(sockfd);
sleep(2);
mwrite( sockfd, buf4, sizeof(buf4));
printf("read 3b ");
read(sockfd);
mwrite( sockfd, buf5, sizeof(buf5));
sleep(2);
mwrite( sockfd, buf6, sizeof(buf6));
printf("read 4 ");
read(sockfd);
mwrite( sockfd, buf7, sizeof(buf7));
sleep(2);
mwrite( sockfd, buf8, sizeof(buf8));
read(sockfd);
mwrite ( sockfd, buffer, 2);
read(sockfd);
return 0;
}
|