Evo, baš sam nešto radio u vezi toga prethodnih dana... SERVER-KLIJENT komunikacija:
SERVER.CPP
------------
Code:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
struct student{
char ime[20];
char prez[20];
char matbr[15];
char JMBG[15];
short kz1;
short kz2;
short lab;
float ukupno;
short ocjena;
};
//treba linkati sa Ws2_32.lib
void win32stuff(){
WSADATA wsaData;
if(WSAStartup(MAKEWORD( 2, 2 ), &wsaData)){
fprintf(stderr, "Ne mogu inicijalizirati mrezu\n");
exit(1);
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
fprintf(stderr, "Ne mogu inicijalizirati prave parametre\n");
WSACleanup( );
exit(1);
}
}
int main(){
int sock, lenght, bodovi = 0, ocjena = 0;
struct sockaddr_in name, client;
char buf[1000];
win32stuff();
sock=socket(AF_INET, SOCK_DGRAM, 0);
if( sock<0 ){
perror("Nece stvoriti socket\n");
exit(1);
}
name.sin_family = AF_INET;
name.sin_addr.s_addr = INADDR_ANY;
name.sin_port=htons(10000);
if( bind(sock, (struct sockaddr*) &name, sizeof(name) )<0 ){
perror("Nece bindati\n");
exit(1);
}
lenght=sizeof(name);
if(getsockname(sock, (struct sockaddr*) &name, &lenght)<0){
perror("Getsocket error\n");
exit(1);
}
printf("Socket je na portu: %d, ja slusam:\n", ntohs(name.sin_port));
while(1){
if( recvfrom(sock, buf, 1024,0, (struct sockaddr*) &client, &lenght)<0 )
perror("Nece ili nema sto za citati\n");
else{
printf("=>%s\n", buf);
bodovi = 0;
ocjena = 0;
// obradi dobivene podatke
// Rezultati(buf, &bodovi, &ocjena);
sprintf(buf, "%s %d %d\n", buf, bodovi, ocjena);
if( sendto(sock, buf, strlen(buf)+1, 0, (struct sockaddr*) &client, sizeof(name))<0 ){
perror("Nece slati\n");
exit(1);
}
}
if(buf[0]=='e' && buf[1]=='n' && buf[2]=='d')
break;
}
closesocket(sock);
return 0;
}
KLIJENT.CPP
-----------
Code:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
//treba linkati sa Ws2_32.lib
void win32stuff(){
WSADATA wsaData;
if(WSAStartup(MAKEWORD( 2, 2 ), &wsaData)){
fprintf(stderr, "Ne mogu inicijalizirati mrezu\n");
exit(1);
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
fprintf(stderr, "Ne mogu inicijalizirati prave parametre\n");
WSACleanup( );
exit(1);
}
}
void main(int argc, char *argv[]){
int sock;
struct sockaddr_in name;
struct hostent *hp;
char buf[1000];
win32stuff();
sock=socket(AF_INET, SOCK_DGRAM, 0);
if(argc<4){
perror("Upotreba: program racunalo port poruka\n");
getchar();
exit(1);
}
if( sock<0 ){
perror("Nece stvoriti socket\n");
exit(1);
}
hp=gethostbyname(argv[1]);
if(hp==0){
perror("%s - nepoznat host!\n");
exit(1);
}
memcpy ( (char*) &name.sin_addr, (char*)hp->h_addr, hp->h_length);
name.sin_family = AF_INET;
name.sin_port=htons(atoi(argv[2]));
if( sendto(sock, argv[3], strlen(argv[3])+1, 0, (struct sockaddr*) &name, sizeof(name))<0 ){
perror("Nece slati\n");
exit(1);
}
if( recv(sock, buf, 1024,0)<0 )
perror("Nece ili nema sto za citati\n");
else
printf("=>%s\n", buf);
closesocket(sock);
}
Ovo sam radio za jedan predmet na faksu pa ćeš dosta toga moći i eliminirati :)
[Ovu poruku je menjao itf dana 10.09.2005. u 09:37 GMT+1]