Freesco, NND, CDN, EOS http://forum.freesco.pl/ |
|
Pomocy przy programowaniu w C .... http://forum.freesco.pl/viewtopic.php?f=28&t=12609 |
Strona 1 z 1 |
Autor: | RufiNs [ wtorek, 30 maja 2006, 02:35 ] |
Tytuł: | Pomocy przy programowaniu w C .... |
Na zaliczenie mam do napisania aplikacje typu Klient-Server na socketach, udalo mi sie cos takiego splodzic. Potrzebuje dopisac do tego logowanie informacji do pliku takich jak: tresc wiadomosci, od kogo i kedy. Czy moglby ktos mi w tym pomoc ?? Probowalem dodac biblioteke <fstream> wywala mi ze nie moze jej znalesc ..... Ponizej wklejam kod serverka: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include "logi.h" //#define MYPORT 22 // the port users will be connecting to #define MAXBUFLEN 500 int main() { int sockfd; struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address information int addr_len, numbytes; char buf[MAXBUFLEN]; int myport; printf("Set Port Number: "); // get port number to work scanf ("%d", &myport); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(myport); // short, network byte order my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } while(1){ addr_len = sizeof(struct sockaddr); if ((numbytes=recvfrom(sockfd,buf, MAXBUFLEN-1, 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } printf("\n\a"); printf("\033[1;31mgot packet from\033[0;0m \033[1;33m %s \033[0;0m \n",inet_ntoa(their_addr.sin_addr)); printf("\033[1;31mpacket is \033[1;33m %d \033[1;31m bytes long \033[0;0m \n",numbytes); buf[numbytes] = '\0'; printf("\033[1;31mMesg: \033[1;33m %s \033[0;0m \n",buf); } } |
Autor: | MAC!EK [ wtorek, 30 maja 2006, 10:14 ] |
Tytuł: | |
deklaracaja i ustawienie wskaźnika: FILE *plik; plik=fopen("log.log","a"); if(plik==NULL) { fprintf(stderr, "Nie mozna otworzyc pliku\n"); return 0; } teraz piszemy tak: albo tak: zamykamy... |
Autor: | RufiNs [ czwartek, 1 czerwca 2006, 05:12 ] |
Tytuł: | |
wielkie THX, uruchomilem i dziala ![]() ![]() Pozdrawiam |
Strona 1 z 1 | Strefa czasowa UTC+2godz. |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |