Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * client.c
- *
- * Copyright 2017 Unknown <s01116@lab4-pc14>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #define SOCKET_ERROR ((int)-1)
- #define SERVER_PORT 1324
- #define MAXSIZE 10
- int main(int argc, char **argv)
- {
- char indirizzoServer[]="127.0.0.1";
- char messaggio[]="ciao";
- char buffer[MAXSIZE];
- char ch,chMaiu;
- int ris,nwrite,len;
- int socketfd;
- struct sockaddr_in locale,remoto;
- locale.sin_family=AF_INET;
- locale.sin_addr.s_addr=htonl(INADDR_ANY);
- locale.sin_port=htons(0);
- remoto.sin_family=AF_INET;
- remoto.sin_addr.s_addr=inet_addr(indirizzoServer);
- remoto.sin_port=htons(SERVER_PORT);
- printf("Creazione del socket()\n");
- socketfd=socket(AF_INET,SOCK_STREAM,0);
- if(socketfd==SOCKET_ERROR)
- {
- printf("fallito il bind(),errore: %d \"%s\"\n",errno,strerror(errno));
- return(1);
- }
- printf("bind()\n");
- ris=bind(socketfd,(struct sockaddr*)&locale,sizeof(locale));
- if(ris==SOCKET_ERROR)
- {
- printf("fallito il bind(),errore: %d \"%s\"\n",errno,strerror(errno));
- return(3);
- }
- printf("connect()\n");
- ris=connect(socketfd,(struct sockaddr*)&remoto,sizeof(remoto));
- if(ris==SOCKET_ERROR)
- {
- printf("fallita la connect(),errore: %d \"%s\"\n",errno,strerror(errno));
- fflush(stdout);
- return(4);
- }
- len=strlen(messaggio);
- printf("\nstringa lunga: %d \n",len);
- for(nwrite=0;nwrite<len;nwrite++)
- {
- write(socketfd,&(messaggio[nwrite]),1);
- if(read(socketfd,&chMaiu,1)>0)
- {
- printf("carattere ricevuto: %c\n",chMaiu);
- buffer[nwrite]=chMaiu;
- }
- }
- close(socketfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement