|
| HOMEPAGE | INDICE FORUM | REGOLAMENTO | ::. | NEI PREFERITI | .:: | RSS Forum | RSS News | NEWS web | NEWS software | |
| PUBBLICITA' | | | ARTICOLI | WIN XP | VISTA | WIN 7 | REGISTRI | SOFTWARE | MANUALI | RECENSIONI | LINUX | HUMOR | HARDWARE | DOWNLOAD | | | CERCA nel FORUM » | |
07-05-2004, 17.21.39 | #16 | |
Gold Member
Registrato: 07-01-2002
Loc.: Milano
Messaggi: 2.863
|
Quota:
Bye P.S.: A mio avviso il carattere restituito non ha alcuna importanza, è quello che personalmente definisco come "schifezza" |
|
07-05-2004, 17.52.39 | #17 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
... ma sai che questa svista proprio non la vedo e ci sto perdendo i capelli?
|
07-05-2004, 18.03.38 | #18 | |
Gold Member
Registrato: 07-01-2002
Loc.: Milano
Messaggi: 2.863
|
Quota:
Usa il debugger ! Bye |
|
07-05-2004, 21.44.28 | #19 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
se non ci riesco, posterò il codice grazie |
10-05-2004, 00.03.11 | #20 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
sorgenti di prog fatti bene con tutti i controlli del caso?
|
10-05-2004, 00.14.35 | #21 |
Linux Supporter
Registrato: 02-12-2000
Loc.: Monza
Messaggi: 1.987
|
Ne ho sicuramente da qualche parte, ma sono per linux/unix..
|
10-05-2004, 15.26.21 | #22 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
vanno bene cmq... devo solo comprendere "la logica" e cosa sbaglio nel disconnettere e riconnettere...
grazie... |
10-05-2004, 15.32.24 | #23 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
..questo è il mio server:
#include <stdio.h> #include <winsock.h> #include <vector> #include <iostream> #include <string> #include <stdlib.h> using namespace std; int PORT = 1978; int MAX_CONNECT = 10; bool bVal = true; struct CLIENT_TYP { bool InUse; SOCKET ClientSocket; struct sockaddr_in ClientAddr; }; SOCKET lstSocket; struct sockaddr_in saServer; vector<CLIENT_TYP> Client; int ClientsConnected = 0; timeval timeout; bool bLog = true; bool bLogError = true; //////////////////////////////////////////////////////// bool Error(char *error) { if (bLogError) printf(error); return true; } bool Log(char *log) { if (bLog) printf(log); return true; } //////////////////////////////////////////////////////// bool Setup() { int nRet; WSADATA wsaData; saServer.sin_family = AF_INET; saServer.sin_addr.s_addr = INADDR_ANY; saServer.sin_port = htons(PORT); Log("\nInitializing Winsock..."); if (WSAStartup(MAKEWORD(1,1), &wsaData)) { Log("FAILED\n"); Error("Error Initializing Winsock\n"); WSACleanup(); return false; } Log("OK\n"); Log("Creating Socket..."); lstSocket = socket(AF_INET, SOCK_STREAM, 0); if (lstSocket == INVALID_SOCKET) { Log("FAILED\n"); Error("Error Creating Socket\n"); WSACleanup(); return false; } Log("OK\n"); Log("Binding Socket..."); nRet = bind(lstSocket, (LPSOCKADDR)&saServer, sizeof(struct sockaddr)); if (nRet == SOCKET_ERROR) { Log("FAILED\n"); Error("Error Binding Socket\n"); WSACleanup(); return false; } Log("OK\n"); Log("Setting up Listening..."); nRet = listen(lstSocket, SOMAXCONN); if (nRet == SOCKET_ERROR) { Log("FAILED\n"); Error("Error Listening\n"); WSACleanup(); return false; } Log("OK\n"); if (!Client.empty()) Client.clear(); Log("\n[ Server Set Up Successfull ]\n"); return true; } //////////////////////////////////////////////////////// int Disconnect(int Index) { closesocket(Client[Index].ClientSocket); Client[Index].InUse = false; ClientsConnected--; return 1; } void Shutdown() { shutdown(lstSocket, 2); for (int i = 0; i < Client.size(); i++) Disconnect(i); WSACleanup(); Log("[ Server Shut Down ]\n\n"); } //////////////////////////////////////////////////////// int Check_Connections() { int nRet; fd_set readfds; int Client_Length; CLIENT_TYP tmpClient; Client_Length = sizeof(tmpClient.ClientAddr); timeout.tv_sec = 0; timeout.tv_usec = 0; if (ClientsConnected < MAX_CONNECT) { FD_ZERO(&readfds); FD_SET(lstSocket, &readfds); nRet = select(lstSocket + 1, &readfds, NULL, NULL, &timeout); if (nRet > 0) { int clIndex = Client.size(); CLIENT_TYP newClient; Client.push_back(newClient); Client[clIndex].ClientSocket = accept(lstSocket, (struct sockaddr*)&Client[clIndex].ClientAddr, &Client_Length); if (Client[clIndex].ClientSocket == INVALID_SOCKET) { Log("\nAttempt to accept incoming connection failed"); return 1; } char *ClientIp = inet_ntoa(Client[clIndex].ClientAddr.sin_addr); Log("\nNew connection made with: "); Log(ClientIp); Log("\n\n"); ClientsConnected++; Client[clIndex].InUse = true; } } return 1; } //////////////////////////////////////////////////////// void Get_Message(int clIndex) { int nRet; char data[1000]; nRet = recv(Client[clIndex].ClientSocket, data, sizeof(data), 0); char *ClientIp = inet_ntoa(Client[clIndex].ClientAddr.sin_addr); if (nRet == 0) { Log("\nClient has disconnected: "), Log(ClientIp), Log("\n"); Disconnect(clIndex); } else { Log("<"), Log(ClientIp), Log("> "); data[nRet] = '\0'; cout << data; if(data[0] == '$' || data[0] == '+'){ shutdown(lstSocket, 0); // no ricezione Disconnect(clIndex); if(data[0] == '$') WSACleanup(); Log("\n\nConnessione chiusa da: "), Log(ClientIp), Log("\n\n"); bVal = false; } else{ int index; for(index = 0; index <= 999; index++){ if(data[index] == '\0'){ char buf2[10] = " --> OK\0"; char *bufPTR; bufPTR = &data[index]; *strcat(bufPTR, buf2); index = 1000; } } send(Client[clIndex].ClientSocket, data,sizeof(data), 0); Log("\n"); } } } //////////////////////////////////////////////////////// void Check_Message() { int nRet; fd_set msg_fds; fd_set err_fds; int nfds; timeout.tv_sec = 0; timeout.tv_usec = 0; FD_ZERO(&msg_fds); FD_ZERO(&err_fds); nfds = 0; for(int i = 0; i < Client.size() { FD_SET(Client[i].ClientSocket, &msg_fds); FD_SET(Client[i].ClientSocket, &err_fds); nfds++; i++; } nRet = select(nfds, &msg_fds, NULL, &err_fds, &timeout); if (nRet > 0) { for(int i = 0; i < Client.size(); i++) { if (FD_ISSET(Client[i].ClientSocket, &err_fds)) { char *ClientIp = inet_ntoa(Client[i].ClientAddr.sin_addr); Log("\nClient has disconnected: "), Log(ClientIp), Log("\n"); Disconnect(i); } if (FD_ISSET(Client[i].ClientSocket, &msg_fds)) { Get_Message(i); } } } } //////////////////////////////////////////////////////// void main() { Setup(); while (bVal) { Check_Connections(); Check_Message(); } Shutdown(); system("PAUSE"); } se non trovate errori posterò anche il client... grazie Ciao |
10-05-2004, 19.44.29 | #24 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
... fatto prove?
|
10-05-2004, 19.55.55 | #25 |
Gold Member
Registrato: 07-01-2002
Loc.: Milano
Messaggi: 2.863
|
Per quel che riguarda me .. l'ho letto .. stasera o domani mattina lo provo
Bye |
10-05-2004, 20.38.26 | #26 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
..grazie...
|
12-05-2004, 15.03.38 | #27 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
... provato?
|
13-05-2004, 18.56.39 | #28 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
?
|
13-05-2004, 19.42.23 | #29 |
Gold Member
WT Expert
Registrato: 09-01-2002
Loc.: None of your business
Messaggi: 5.505
|
Me lo sono copiato e compilato poco fa.
Mi faresti la cortesia di postare anche il client, per favore ? |
13-05-2004, 20.41.06 | #30 |
Hero Member
Registrato: 09-05-2002
Loc.: COMO
Messaggi: 1.135
|
client:
#include <windows.h> #include <sys/types.h> #include <winsock.h> #include <stdio.h> #include <string.h> #define PROTOPORT 1978 extern int errno; char localhost[] = "localhost"; void main(argc, argv) int argc; char *argv[]; { struct hostent *ptrh; struct protoent *ptrp; struct sockaddr_in sad; int sd; int port; char *host; int n=0; char buf[1000]; char cDaInv=0; #ifdef WIN32 WSADATA wsaData; WSAStartup(0x0101, &wsaData); #endif memset((char *)&sad,0,sizeof(sad)); sad.sin_family = AF_INET; if (argc > 2) { port = atoi(argv[2]); } else { port = PROTOPORT; } if (port > 0) sad.sin_port = htons((u_short)port); else { fprintf(stderr,"Bad Port Number %s\n",argv[2]); exit(1); } if (argc > 1) { host = argv[1]; } else { host = localhost; } 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, ptrp->p_proto); if (sd < 0) { fprintf(stderr, "Socket Creation Failed\n"); exit(1); } if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) { fprintf(stderr,"Connect Failed\n"); exit(1); } printf("\n\n DCN-Server\n\t+ Termina questo programma\n\t$ Termina anche il client\n\n - Connessione Stabilita\n"); //ECHO do{ printf("\nINVIA: "); gets(buf); if(buf[0] != '+'){ send(sd,buf,strlen(buf),0); n = recv(sd, buf, sizeof(buf), 0); printf("%s\n",buf); } else n = 0; }while (n > 0); closesocket(sd); printf("\nConnessione Conclusa, Client %s\n\n", buf[0] == '+' ? "ancora attivo" : "disattivato"); system("PAUSE"); exit(0); } |
Utenti attualmente attivi che stanno leggendo questa discussione: 1 (0 utenti e 1 ospiti) | |
Strumenti discussione | |
|
|
Discussioni simili | ||||
Discussione | Autore discussione | Forum | Risposte | Ultimo messaggio |
[MDV] 2006 - problema kde | juggler | Linux e altri Sistemi Operativi | 4 | 29-12-2006 21.06.08 |
[XP/SP2] Controllo in corso del file System | primoair | Windows 7/Vista/XP/ 2003 | 2 | 28-02-2006 22.13.01 |
[Valutazione] cpu+mobo+ram+scsi+sata+video | Don`t`Ask | Mercatino Usato | 2 | 14-11-2004 18.54.06 |
controllo D:/ al boot | slivio... | Windows 7/Vista/XP/ 2003 | 2 | 21-11-2003 01.32.59 |