|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Sep 2003
Città: Tradate
Messaggi: 396
|
[C] Socket
Codice:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #define PORT 5091 int s, new_sd; struct sockaddr_in myaddr, remoteaddr; char buff; int clientlen=sizeof(remoteaddr); int pid; int main() { if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket()"); exit(-1); } myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = htonl(INADDR_ANY); myaddr.sin_port = htons(PORT); memset(&(myaddr.sin_zero), 0, sizeof(myaddr.sin_zero)); if (bind(s, &myaddr, sizeof(myaddr)) == -1) { perror("bind()"); exit(-1); } if (listen(s, 5) == -1) { perror("listen()"); exit(-1); } while(1){ fflush(stdout); new_sd=accept(s,&remoteaddr,&clientlen); //printf("%d",ntohs(remoteaddr.sin_port)); printf("aaa"); pid=fork(); if(pid==0) { //do //{ recv(new_sd,&buff,sizeof(buff),0); switch(buff) { case '0': break; case '1': printf("1"); case '2': printf("2"); } //}while(buff!='0'); printf("%c",buff); close(new_sd); exit(0); } else close(new_sd); } return (0); } Grazie.. ![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Quale printf ?
|
![]() |
![]() |
![]() |
#3 |
Senior Member
Iscritto dal: Sep 2003
Città: Tradate
Messaggi: 396
|
printf("aaa");
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Jan 2002
Città: Napoli
Messaggi: 1727
|
nei case dello switch se non metti un break al termine di ogni case, lui esegue le istruzioni anche dei case successivi.
dai un occhio alla sintassi di switch case
__________________
Se buttassimo in un cestino tutto ciò che in Italia non funziona cosa rimarrebbe? Il cestino. |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 06:32.