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 154 155 156 157 158 159 160 161 162 163 164 165
| /* client.c - TCP */
#ifndef unix
#define WIN32
#include <windows.h>
#include <winsock.h>
#else
#define closesocket close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif
#include <stdio.h>
#include <string.h>
#define PROTOPORT 50001
extern int errno;
char localhost[20];
main(argc, argv)
int argc;
char *argv[];
{
/* void exit(int status){}*/
struct hostent *ptrh;
struct protoent *ptrp;
struct sockaddr_in sad;
struct servent *service;
int sd;
int port;
char *host;
int n;
char buf [1000];
memset((char *)&sad,0,sizeof(sd));
sad.sin_family = AF_INET;
service=getservbyname("daytime","tcp");
port = service->s_port;
if (port > 0)
sad.sin_port = port;
else{
fprintf(stderr,"BAD PORT NUMBER %s\n",argv[2]);
exit(1);
}
host = argv[1];
puts(host);
ptrh = gethostbyname(host);
if( ((char *)ptrh) == NULL) {
fprintf(stderr,"invalid host: %s\n", host);
exit(1);
}
memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);
if ( ((int)(ptrp=getprotobyname("tcp"))) == 0) {
fprintf(stderr, "cannot map \"tcp\" to protocol number");
exit(1);
}
sd = socket(PF_INET, SOCK_STREAM, 0);
if (sd<0) {
fprintf(stderr, "socket creation failed\n");
exit(1);
}
if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0)
{
fprintf(stderr, "connect faibled\n");
exit(1);
}
/* -------------------------------Saisie du service à réaliser-----------------------------------------*/
printf("saisie du message\n");
scanf("%s",&buf);
send(sd,buf,sizeof(buf),0);
n=recv(sd,buf, sizeof(buf),0);
if(n!=0){
printf("Message reçu est: %s\n", buf);
}
else{
printf("Message non reçu\n");}
/*----------------------------------------------------------------------------*/
closesocket(sd);
} |
Partager