Lab 2
#include <windows.h>
#include <nb30.h>
#include <stdio.h>
UCHAR OpisBledu (UCHAR err)
{
switch (err)
{
case NRC_BUFLEN: printf("An illegal buffer . \n");
.
.
.
break;
}
}
UCHAR ResetAdapter(NCB *blok)
{
memset(blok, 0, sizeof(blok));
blok->ncb_command = 0x32;
blok->ncb_lana_num = 0x03;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR AddName (UCHAR naz[16], NCB *blok)
{
memset(blok->ncb_name,' ',NCBNAMSZ);
memcpy(blok->ncb_name,naz,15);
blok->ncb_lana_num = 0x03;
blok->ncb_command = NCBADDNAME;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR DeleteName (UCHAR naz[16], NCB *blok)
{
memcpy(blok->ncb_name,naz,15);
blok->ncb_lana_num = 0x03;
blok->ncb_command = NCBDELNAME;
Netbios(blok);
return blok->ncb_retcode;
}
int main(void)
{
UCHAR err, nr_odb, odb[16];
NCB blok; /* typ NCB jest predefiniowany */
err = ResetAdapter(&blok); /* reset netbiosa */
if (err != NRC_GOODRET)
{
printf("Blad resetowania!\n");
OpisBledu(err);
system("pause");
exit(1);
}
printf("Podaj nazwe wezla-odbiorcy (max. 15 znakow):");
scanf("%s",odb);
err = AddName(odb, &blok); /* dodanie unikatowej nazwy dla odbiorcy */
if (err != NRC_GOODRET)
{
printf("Blad dodawania nazwy odbiorcy!\n");
OpisBledu(err);
system("pause");
exit(1);
}
// nr_odb = blok.ncb_num; /* zapamiętujemy przydzielony do nazwy numer */
//system("PAUSE");
system("nbtstat -n");
system("PAUSE");
err = DeleteName(odb,&blok);
if (err != NRC_GOODRET)
{
printf("Blad usuwania wezla:\n");
OpisBledu(err);
system("pause");
exit(1);
}
system("nbtstat -n");
system("PAUSE");
return 0;
}
Lab 3
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <nb30.h>
typedef
struct TNCB
{
UCHAR ncb_command;
UCHAR ncb_retcode;
UCHAR ncb_lsn;
UCHAR ncb_num;
PUCHAR ncb_buffer;
WORD ncb_lenght;
UCHAR ncb_callname[NCBNAMSZ];
UCHAR ncb_name[NCBNAMSZ];
UCHAR ncb_rto;
UCHAR ncb_sto;
void (*ncb_post)(struct _NCB *);
UCHAR ncb_lana_num;
UCHAR ncb_cmd_cplt;
UCHAR ncb_reserve[10];
HANDLE ncb_event;
};
UCHAR resetuj_NCB(struct TNCB *blok)
{
memset(blok,0,sizeof(blok));
blok->ncb_lana_num =0x03;
blok->ncb_command= NCBRESET;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR dodaj_NCB(struct TNCB *blok, UCHAR naz[16])
{
memset(blok->ncb_name,' ',NCBNAMSZ);
memcpy(blok->ncb_name,naz,15);
blok->ncb_lana_num = 0x03;
blok->ncb_command = NCBADDNAME;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR usun_NCB(struct TNCB *blok, UCHAR naz[16])
{
memcpy(blok->ncb_name,naz,15);
blok->ncb_lana_num = 0x03;
blok->ncb_command = NCBDELNAME;
Netbios(blok);
return blok->ncb_retcode;
}
void opis_bledu(UCHAR err)
{
switch (err)
{
Błędy …
break;
}
}
int main(int argc, char *argv[])
{
struct TNCB bl;
UCHAR err;
CHAR nazwa[16];
err = resetuj_NCB(&bl);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie czyszczenia:\n");
opis_bledu(err);
}
else printf("Czyszczenie zakonczone powodzeniem\n");
printf("Prosze podac nazwe wezla odbiorcy(max 15 znakow): ");
fgets(nazwa,15,stdin);
err = dodaj_NCB(&bl,nazwa);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie dodania wezla:\n");
opis_bledu(err);
}
else printf("Dodanie wezla zakonczone powodzeniem\n");
system("pause");
err = usun_NCB(&bl,nazwa);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie usuniecia wezla:\n");
opis_bledu(err);
}
else printf("Usuniecie wezla zakonczone powodzeniem\n");
system("PAUSE");
return 0;
}
Lab4.
#include <windows.h>
#include <nb30.h>
#include <stdio.h>
#include <string.h>
UCHAR OpisBledu (UCHAR err)
{
switch (err)
{
case NRC_PENDING: printf("An asynchronous operation is not yet finished. \n"); break;
}
}
UCHAR ResetAdapter(NCB *blok)
{
memset(blok, '\0', sizeof(*blok));
blok->ncb_command = 0;
blok->ncb_command = NCBRESET;
blok->ncb_lana_num = 0x04;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR AddName (UCHAR naz[16], NCB *blok)
{
memset(blok,'\0', sizeof(*blok));
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = 0x04;
blok->ncb_command = NCBADDNAME;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR AddGroupName (UCHAR naz[16], NCB *blok)
{
memset(blok,'\0', sizeof(*blok));
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = 0x04;
blok->ncb_command = NCBADDGRNAME;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR DeleteName (UCHAR naz[16], NCB *blok)
{
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = 0x04;
blok->ncb_command = NCBDELNAME;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR Call (NCB *blok, UCHAR naz[16], UCHAR callnaz[16])
{
memcpy(blok->ncb_callname,callnaz,strlen(callnaz));
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = 0x04;
blok->ncb_rto = 99;
blok->ncb_sto = 99;
blok->ncb_command = NCBCALL;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR Listen (NCB *blok, UCHAR naz[16], UCHAR callnaz[16])
{
memcpy(blok->ncb_callname,callnaz,strlen(callnaz));
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = 0x04;
blok->ncb_rto = 99;
blok->ncb_sto = 99;
blok->ncb_command = NCBLISTEN;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR SendDatagram(NCB *blok, char buf[50], UCHAR odb[16], UCHAR nr_nad)
{
memset(blok,'\0',sizeof(*blok));
blok->ncb_lana_num = 0x04;
blok->ncb_command = NCBDGSEND;
memcpy(blok->ncb_callname,odb,strlen(odb));
blok->ncb_num = nr_nad;
blok->ncb_buffer = buf;
blok->ncb_length = strlen(buf);
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR ReceiveDatagram(NCB *blok, char buf[50], UCHAR nr_odb)
{
UCHAR od_kogo[16];
memset(blok,'\0',sizeof(*blok));
blok->ncb_lana_num = 0x04;
blok->ncb_command = NCBDGRECV;
blok->ncb_num = nr_odb;
memset(buf,'\0',50);
blok->ncb_buffer = buf;
blok->ncb_length = 50;
printf("Oczekuje na datagram... \n");
Netbios(blok);
if (blok->ncb_retcode == 0)
{
memcpy(od_kogo,blok->ncb_callname,15);
printf("Odebrano wiadomosc od %s : \n",od_kogo);
printf("%s",buf);
printf("\n");
}
return blok->ncb_retcode;
}
UCHAR SendBroadcast(NCB *blok, char buf[50], UCHAR nr_nad, UCHAR nazw_odb[16])
{
memset(blok,'\0',sizeof(*blok));
blok->ncb_lana_num = 0x04;
blok->ncb_command = NCBDGSEND;
memcpy(blok->ncb_callname,nazw_odb,strlen(nazw_odb));
blok->ncb_num = nr_nad;
blok->ncb_buffer = buf;
blok->ncb_length = strlen(buf);
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR ReceiveBroadcast(NCB *blok,char buf[50],UCHAR nr_odb)
{
UCHAR od_kogo[16];
memset(blok,'\0',sizeof(*blok));
blok->ncb_lana_num = 0x04;
blok->ncb_command = NCBDGRECV;
memset(buf,'\0',50);
blok->ncb_buffer = buf;
blok->ncb_length = 50;
blok->ncb_num = nr_odb;
printf("Oczekuje na datagram broadcastowy... \n");
Netbios(blok);
if (blok->ncb_retcode == 0)
{
memcpy(od_kogo,blok->ncb_callname,15);
printf("Odebrano wiadomosc od %s : \n",od_kogo);
printf("%s",buf);
printf("\n");
}
return blok->ncb_retcode;
}
void usun_kropki(char *nad, int a)
{
int i;
for(i=a;i<16;i++)
{
*(nad+i) = ' ';
}
}
int main(void)
{
UCHAR err, nr_nad, odb[16], callnaz[16], nad[16];
char bufor[50];
int wyb, dlugosc;
NCB blok; /* typ NCB jest predefiniowany */
err = ResetAdapter(&blok); /* reset netbiosa */
if (err != NRC_GOODRET)
{
printf("Blad resetowania!\n");
OpisBledu(err);
system("pause");
exit(1);
}
printf("Podaj nazwe wezla (max. 15 znakow):");
fflush(stdin);
scanf("%16s", nad);
dlugosc = strlen(nad);
usun_kropki(nad,dlugosc);
int wyb1;
printf("Groupname(1) czy name(2)?\n");
scanf("%d", &wyb1);
switch (wyb1){
case 1: { err = AddGroupName(nad, &blok); /* dodanie unikatowej nazwy dla odbiorcy */
if (err != NRC_GOODRET)
{
printf("Blad dodawania nazwy odbiorcy!\n");
OpisBledu(err);
system("pause");
exit(1);
}
else { printf("Dodano nazwe wezla\n");break;}}
case 2: { err = AddName(nad, &blok); /* dodanie unikatowej nazwy dla odbiorcy */
if (err != NRC_GOODRET)
{
printf("Blad dodawania nazwy odbiorcy!\n");
OpisBledu(err);
system("pause");
exit(1);
}
else {printf("Dodano nazwe wezla\n");break;}}
}
nr_nad = blok.ncb_num;
system("PAUSE");
system("nbtscan.exe -f 81.26.7.150 ");
printf("Chcesz wyslac(1) czy odebrac datagram(2) \n");
printf(" wyslac(3) czy odebrac datagram broadcastowy(4): ");
scanf("%d", &wyb);
if (wyb == 1)
{
printf("Prosze podac nazwe odbiorcy: ");
scanf("%16s", odb);
dlugosc = strlen(odb);
usun_kropki(odb,dlugosc);
printf("Prosze podac tekst do wyslania: ");
fflush(stdin);
scanf("%[^\n]", bufor);
err = SendDatagram(&blok, bufor, odb, nr_nad);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie wyslania wiadomosci:\n");
OpisBledu(err);
}
else
{
printf("Wyslanie wiadomosci zakonczone powodzeniem\n");
}
}
else if (wyb == 2)
{
err = ReceiveDatagram(&blok, bufor, nr_nad);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie odebrania wiadomosci:\n");
OpisBledu(err);
}
}
else if (wyb == 3)
{
printf("Prosze podac tekst do wyslania: ");
fflush(stdin);
scanf("%[^\n]", bufor);
UCHAR nazw_odb[16];
printf("Podaj nazwe odbiorcy\n");
scanf("%16s",&nazw_odb);
err = SendBroadcast(&blok, bufor, nr_nad, nazw_odb);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie wyslania wiadomosci:\n");
OpisBledu(err);
}
else
{
printf("Wyslanie wiadomosci zakonczone powodzeniem\n");
}
}
else if (wyb == 4)
{
err = ReceiveBroadcast(&blok,bufor, nr_nad);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie odebrania wiadomosci:\n");
OpisBledu(err);
}
}
system("PAUSE");
err = DeleteName(nad,&blok);
if (err != NRC_GOODRET)
{
printf("Blad usuwania wezla:\n");
OpisBledu(err);
system("pause");
exit(1);
}
else printf("Usunieto nazwe wezla\n");
system("nbtscan.exe -f 81.26.7.150 ");
system("PAUSE");
return 0;
}
Lab 5.
#include <windows.h>
#include <nb30.h>
#include <stdio.h>
#include <string.h>
#define lana 0x03
UCHAR OpisBledu (UCHAR err)
{
switch (err)
{
case NRC_BUFLEN: printf("An illegal buffer length was supplied. \n"); break;
case NRC_ILLCMD: printf("An illegal command was supplied. \n"); break;
case NRC_CMDTMO: printf("The command was timed out. \n"); break;
case NRC_INCOMP: printf("The message was incomplete. The application is to issue another command. \n"); break;
case NRC_BADDR: printf("The buffer address was illegal. \n"); break;
case NRC_SNUMOUT: printf("The session number was out of range. \n"); break;
case NRC_NORES: printf("No resource was available. \n"); break;
case NRC_SCLOSED: printf("The session was closed. \n"); break;
case NRC_CMDCAN: printf("The command was canceled. \n"); break;
case NRC_DUPNAME: printf("A duplicate name existed in the local name table. \n"); break;
case NRC_NAMTFUL: printf("The name table was full. \n"); break;
case NRC_ACTSES: printf("The command finished; the name has active sessions and is no longer registered. \n"); break;
case NRC_LOCTFUL: printf("The local session table was full. \n"); break;
case NRC_REMTFUL: printf("The remote session table was full. The request to open a session was rejected. \n"); break;
case NRC_ILLNN: printf("An illegal name number was specified. \n"); break;
case NRC_NOCALL: printf("The system did not find the name that was called. \n"); break;
case NRC_NOWILD: printf("Wildcards are not permitted in the ncb_name member. \n"); break;
case NRC_INUSE: printf("The name was already in use on the remote adapter. \n"); break;
case NRC_NAMERR: printf("The name was deleted. \n"); break;
case NRC_SABORT: printf("The session ended abnormally. \n"); break;
case NRC_NAMCONF: printf("A name conflict was detected. \n"); break;
case NRC_IFBUSY: printf("The interface was busy. \n"); break;
case NRC_TOOMANY: printf("Too many commands were outstanding; the application can retry the command later. \n"); break;
case NRC_BRIDGE: printf("The ncb_lana_num member did not specify a valid network number. \n"); break;
case NRC_CANOCCR: printf("The command finished while a cancel operation was occurring. \n"); break;
case NRC_CANCEL: printf("The NCBCANCEL command was not valid; the command was not canceled. \n"); break;
case NRC_DUPENV: printf("The name was defined by another local process. \n"); break;
case NRC_ENVNOTDEF: printf("The environment was not defined. A reset command must be issued. \n"); break;
case NRC_OSRESNOTAV: printf("Operating system resources were exhausted. The application can retry and later. \n"); break;
case NRC_MAXAPPS: printf("The maximum number of applications was exceeded. \n"); break;
case NRC_NOSAPS: printf("No service access points (SAPs) were available for NetBIOS. \n"); break;
case NRC_NORESOURCES: printf("The requested resources were not available. \n"); break;
case NRC_INVADDRESS: printf("The NCB address was not valid. \n"); break;
case NRC_INVDDID: printf("The NCB DDID was invalid. \n"); break;
case NRC_LOCKFAIL: printf("The attempt to lock the user area failed. \n"); break;
case NRC_OPENERR: printf("An error occurred during an open operation being performed by the device driver. This return code is not part of the IBM NetBIOS 3.0 specification. \n"); break;
case NRC_SYSTEM: printf("A system error occurred. \n"); break;
case NRC_PENDING: printf("An asynchronous operation is not yet finished. \n"); break;
}
}
UCHAR ResetAdapter(NCB *blok)
{
memset(blok, '\0', sizeof(*blok));
blok->ncb_command = 0;
blok->ncb_command = NCBRESET;
blok->ncb_lana_num = lana;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR AddName (UCHAR naz[16], NCB *blok)
{
memset(blok,'\0', sizeof(*blok));
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = lana;
blok->ncb_command = NCBADDNAME;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR AddGrupName (UCHAR naz[16], NCB *blok)
{
memset(blok,'\0', sizeof(*blok));
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = lana;
blok->ncb_command = NCBADDGRNAME;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR DeleteName (UCHAR naz[16], NCB *blok)
{
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = lana;
blok->ncb_command = NCBDELNAME;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR Call (NCB *blok, UCHAR naz[16], UCHAR callnaz[16])
{
memcpy(blok->ncb_callname,callnaz,strlen(callnaz));
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = lana;
blok->ncb_rto = 99;
blok->ncb_sto = 99;
blok->ncb_command = NCBCALL;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR Listen (NCB *blok, UCHAR naz[16], UCHAR callnaz[16])
{
memcpy(blok->ncb_callname,callnaz,strlen(callnaz));
memcpy(blok->ncb_name,naz,strlen(naz));
blok->ncb_lana_num = lana;
blok->ncb_rto = 99;
blok->ncb_sto = 99;
blok->ncb_command = NCBLISTEN;
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR SendDatagram(NCB *blok, char buf[50], UCHAR odb[16], UCHAR nr_nad)
{
memset(blok,'\0',sizeof(*blok));
blok->ncb_lana_num = lana;
blok->ncb_command = NCBDGSEND;
memcpy(blok->ncb_callname,odb,strlen(odb));
blok->ncb_num = nr_nad;
blok->ncb_buffer = buf;
blok->ncb_length = strlen(buf);
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR ReceiveDatagram(NCB *blok, char buf[50], UCHAR nr_odb)
{
UCHAR od_kogo[16];
memset(blok,'\0',sizeof(*blok));
blok->ncb_lana_num = lana;
blok->ncb_command = NCBDGRECV;
blok->ncb_num = nr_odb;
memset(buf,'\0',50);
blok->ncb_buffer = buf;
blok->ncb_length = 50;
printf("Oczekuje na datagram... \n");
Netbios(blok);
if (blok->ncb_retcode == 0)
{
memcpy(od_kogo,blok->ncb_callname,15);
printf("Odebrano wiadomosc od %s : \n",od_kogo);
printf("%s",buf);
printf("\n");
}
return blok->ncb_retcode;
}
UCHAR SendBroadcast(NCB *blok, char buf[50], UCHAR nr_nad)
{
memset(blok,'\0',sizeof(*blok));
blok->ncb_lana_num = lana;
blok->ncb_command = NCBDGSENDBC;
blok->ncb_num = nr_nad;
blok->ncb_buffer = buf;
blok->ncb_length = strlen(buf);
Netbios(blok);
return blok->ncb_retcode;
}
UCHAR ReceiveBroadcast(NCB *blok,char buf[50],UCHAR nr_odb)
{
UCHAR od_kogo[16];
memset(blok,'\0',sizeof(*blok));
blok->ncb_lana_num = lana;
blok->ncb_command = NCBDGRECVBC;
memset(buf,'\0',50);
blok->ncb_buffer = buf;
blok->ncb_length = 50;
blok->ncb_num = nr_odb;
printf("Oczekuje na datagram broadcastowy... \n");
Netbios(blok);
if (blok->ncb_retcode == 0)
{
memcpy(od_kogo,blok->ncb_callname,15);
printf("Odebrano wiadomosc od %s : \n",od_kogo);
printf("%s",buf);
printf("\n");
}
return blok->ncb_retcode;
}
void usun_kropki(char *nad, int a)
{
int i;
for(i=a;i<16;i++)
{
*(nad+i) = ' ';
}
}
int main(void)
{
UCHAR err, nr_nad, odb[16], callnaz[16], nad[16];
char bufor[50];
int wyb, dlugosc;
NCB blok; /* typ NCB jest predefiniowany */
err = ResetAdapter(&blok); /* reset netbiosa */
if (err != NRC_GOODRET)
{
printf("Blad resetowania!\n");
OpisBledu(err);
system("pause");
exit(1);
}
printf("Chcesz dodać nazwe urzytkownika(1) czy grupy(2): ");
scanf("%d", &wyb);
printf("Podaj nazwe wezla (max. 15 znakow):");
fflush(stdin);
scanf("%16s", nad);
dlugosc = strlen(nad);
usun_kropki(nad,dlugosc);
if (wyb == 1)
{
err = AddName(nad, &blok); /* dodanie unikatowej nazwy dla odbiorcy */
if (err != NRC_GOODRET)
{
printf("Blad dodawania nazwy!\n");
OpisBledu(err);
system("pause");
exit(1);
}
else printf("Dodano nazwe wezla\n");
}
else if (wyb == 2)
{
err = AddGrupName(nad, &blok); /* dodanie unikatowej nazwy dla odbiorcy */
if (err != NRC_GOODRET)
{
printf("Blad dodawania nazwy!\n");
OpisBledu(err);
system("pause");
exit(1);
}
else printf("Dodano nazwe wezla\n");
}
else
{
printf("\n;(\n");
system("pause");
exit(1);
}
nr_nad = blok.ncb_num;
system("PAUSE");
system("nbtstat -n");
printf("Chcesz wyslac(1) czy odebrac datagram(2) \n");
printf(" wyslac(3) czy odebrac datagram broadcastowy(4): ");
scanf("%d", &wyb);
if (wyb == 1)
{
printf("Prosze podac nazwe odbiorcy: ");
scanf("%16s", odb);
dlugosc = strlen(odb);
usun_kropki(odb,dlugosc);
printf("Prosze podac tekst do wyslania: ");
fflush(stdin);
scanf("%[^\n]", bufor);
err = SendDatagram(&blok, bufor, odb, nr_nad);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie wyslania wiadomosci:\n");
OpisBledu(err);
}
else
{
printf("Wyslanie wiadomosci zakonczone powodzeniem\n");
}
}
else if (wyb == 2)
{
err = ReceiveDatagram(&blok, bufor, nr_nad);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie odebrania wiadomosci:\n");
OpisBledu(err);
}
}
else if (wyb == 3)
{
printf("Prosze podac tekst do wyslania: ");
fflush(stdin);
scanf("%[^\n]", bufor);
err = SendBroadcast(&blok, bufor, nr_nad);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie wyslania wiadomosci:\n");
OpisBledu(err);
}
else
{
printf("Wyslanie wiadomosci zakonczone powodzeniem\n");
}
}
else if (wyb == 4)
{
err = ReceiveBroadcast(&blok,bufor, nr_nad);
if (err != NRC_GOODRET)
{
printf("Niepowodzenie odebrania wiadomosci:\n");
OpisBledu(err);
}
}
system("PAUSE");
err = DeleteName(nad,&blok);
if (err != NRC_GOODRET)
{
printf("Blad usuwania wezla:\n");
OpisBledu(err);
system("pause");
exit(1);
}
else printf("Usunieto nazwe wezla\n");
system("nbtstat -n");
system("PAUSE");
return 0;
}
Lab6.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <winsock2.h>
void ErrorCode ()
{
int err = WSAGetLastError();
switch (err)
{
case 11031: printf("Winsock error 11031: WSA_QOS_RESERVED_PETYPE Reserved policy QOS element type.\n"); break;
}
}
void ZawartoscWSADATA(WSADATA wsaData)
{
printf("%s\n",&wsaData.wVersion);
printf("%s\n",&wsaData.wHighVersion);
printf("%s\n",&wsaData.szDescription);
printf("%s\n",&wsaData.szSystemStatus);
printf("%i\n",&wsaData.iMaxSockets);
printf("%i\n",&wsaData.iMaxUdpDg);
printf("%i\n",&wsaData.lpVendorInfo);
}
void gethost(struct hostent *n)
{
printf("\nHost name : %s\n", n->h_name);
printf("Alias list : %s\n", *(n->h_aliases));
printf("Host address type : %i\n", n->h_addrtype);
printf("Length of address : %i\n", n->h_length);
printf("List of addresses : %s\n", *(n->h_addr_list));
//printf("IP Address : %s\n\n", inet_ntoa(*((struct in_addr *)n->h_addr)));
}
int main(void)
{
int len, type;
char name[32];
WSADATA wsaData;
if (WSAStartup(257, &wsaData) != 0)
{
printf("Blad inicjalizacji WSA\n");
system("pause");
exit(1);
}
ZawartoscWSADATA(wsaData);
gethostname(name, 32);
printf("%s\n",&name);
ErrorCode ();
gethost(gethostbyname(name));
ErrorCode ();
struct hostent *n = gethostbyname(name);
gethost(gethostbyaddr(n->h_addr, 4, 2));
ErrorCode ();
WSACleanup();
ErrorCode ();
system("pause");
}
Lab 7.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <winsock2.h>
void ErrorCode ()
{
int err = WSAGetLastError();
switch (err)
{
WSA_QOS_RESERVED_PETYPE Reserved policy QOS element type.\n"); break;
}
}
void ZawartoscWSADATA(WSADATA wsaData)
{
printf("%s\n",&wsaData.wVersion);
printf("%s\n",&wsaData.wHighVersion);
printf("%s\n",&wsaData.szDescription);
printf("%s\n",&wsaData.szSystemStatus);
printf("%i\n",&wsaData.iMaxSockets);
printf("%i\n",&wsaData.iMaxUdpDg);
printf("%i\n",&wsaData.lpVendorInfo);
}
void gethost(struct hostent *n)
{
printf("\nHost name : %s\n", n->h_name);
printf("Alias list : %s\n", *(n->h_aliases));
printf("Host address type : %i\n", n->h_addrtype);
printf("Length of address : %i\n", n->h_length);
printf("List of addresses : %s\n", *(n->h_addr_list));
//printf("IP Address : %s\n\n", inet_ntoa(*((struct in_addr *)n->h_addr)));
}
int main(void)
{
int len, type;
char name[32];
WSADATA wsaData;
if (WSAStartup(257, &wsaData) != 0)
{
printf("Blad inicjalizacji WSA\n");
system("pause");
exit(1);
}
ZawartoscWSADATA(wsaData);
gethostname(name, 32);
printf("%s\n",&name);
ErrorCode ();
gethost(gethostbyname(name));
ErrorCode ();
struct hostent *n = gethostbyname(name);
gethost(gethostbyaddr(n->h_addr, 4, 2));
ErrorCode ();
WSACleanup();
ErrorCode ();
system("pause");
}
Lab 8
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <winsock2.h>
void ErrorCode ()
{
int err = WSAGetLastError();
switch (err)
{
case 0: printf("Winsock error 0: No error\n"); break;
case 11031: printf("Winsock error 11031: WSA_QOS_RESERVED_PETYPE Reserved policy QOS element type.\n"); break;
}
WSACleanup();
system("pause");
exit(1);
}
void dane(int *p, char *ip, char *buf)
{
do
{
printf("Podaj numer portu nasluchu (0-65535): ");
scanf("%d",p);
}
while (*p<0 || *p>65535);
printf("Podaj adres IP odbiorcy, badz nazwe hosta: ");
scanf("%64s",ip);
printf("Podaj wiadomosc do wyslania: ");
fflush(stdin);
scanf("%100[^\n]s", buf);
}
void Odbiorca()
{
SOCKET odbiorca;
int port, dl, t;
struct sockaddr_in odb, nad;
char bufor[100] = "0";
odbiorca = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);
if (odbiorca == SOCKET_ERROR)
{
printf("Nie mozna stworzyc gniazdka\n");
ErrorCode();
}
memset(&odb, 0, sizeof(odb));
do
{
printf("Podaj numer portu nasluchu (0-65535): ");
scanf("%d",&port);
}
while (port<0 || port>65535);
odb.sin_addr.s_addr = htonl(INADDR_ANY);
odb.sin_family = AF_INET;
odb.sin_port = htons(port);
if ( (bind(odbiorca, (struct sockaddr*)&odb, sizeof(odb))) == SOCKET_ERROR)
{
printf("Nie mozna skonfigurowac gniazdka\n");
closesocket(odbiorca);
ErrorCode();
}
printf("Oczekuje na datagram...\n");
do
t = recvfrom(odbiorca, bufor, 100, 0, (struct sockaddr*)&nad, &dl);
while(t == -1);
printf("Adres IP i port nadawcy: %s:%d\n", inet_ntoa(nad.sin_addr),htons(nad.sin_port));
printf("Odebrana wiadomosc: %s\n",bufor);
closesocket(odbiorca);
}
void Nadawca()
{
SOCKET nadawca;
struct sockaddr_in nad, odb;
int port;
struct hostent *host;
char ipodb[64], bufor[100];
nadawca = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(nadawca == INVALID_SOCKET)
{
printf("Nie mozna stworzyc gniazdka\n");
ErrorCode();
}
memset(&nad, 0, sizeof(nad));
nad.sin_family = AF_INET;
nad.sin_port = htons(0);
nad.sin_addr.s_addr = htonl(INADDR_ANY);
if ( (bind(nadawca, (struct sockaddr*)&nad, sizeof(nad))) == SOCKET_ERROR)
{
printf("Nie mozna skonfigurowac gniazdka\n");
closesocket(nadawca);
ErrorCode();
}
dane(&port, ipodb, bufor);
memset(&odb, 0, sizeof(odb));
odb.sin_family = AF_INET;
odb.sin_port = htons(port);
odb.sin_addr.s_addr = inet_addr(ipodb);
if (odb.sin_addr.s_addr == INADDR_NONE)
{
host = gethostbyname(ipodb);
if (host == NULL)
{
printf("Nie mozna skonwertowac adresu\n");
closesocket(nadawca);
ErrorCode();
}
memcpy(&odb.sin_addr, host->h_addr_list[0], host->h_length);
}
sendto(nadawca, bufor, strlen(bufor), 0, (struct sockaddr*)&odb, sizeof(odb));
closesocket(nadawca);
}
int main(void)
{
int wyb;
char name[32];
WSADATA wsaData;
WORD version;
version = MAKEWORD(2,0);
if (WSAStartup(version, &wsaData) != 0)
{
printf("Blad inicjalizacji WSA\n");
ErrorCode();
}
printf("Chcesz odebrac(1) czy wyslac(2) wiadomosc? ");
scanf("%d",&wyb);
if (wyb==1) Odbiorca();
if (wyb==2) Nadawca();
WSACleanup();
system("pause");
}
Lab 9
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <winsock2.h>
void ErrorCode ()
{
int err = WSAGetLastError();
switch (err)
{
case 0: printf("Winsock error 0: No error\n"); break;
}
WSACleanup();
system("pause");
exit(1);
}
void dane(int *p, char *ip, char *buf)
{
do
{
printf("Podaj numer portu nasluchu (0-65535): ");
scanf("%d",p);
}
while (*p<0 || *p>65535);
printf("Podaj adres IP odbiorcy, badz nazwe hosta: ");
scanf("%64s",ip);
printf("Podaj wiadomosc do wyslania: ");
fflush(stdin);
scanf("%100[^\n]s", buf);
}
void Odbiorca()
{
SOCKET odbiorca, nadawca;
int port, dl;
struct sockaddr_in odb, nad;
char bufor[100] = "0";
odbiorca = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
if (odbiorca == SOCKET_ERROR)
{
printf("Nie mozna stworzyc gniazdka\n");
ErrorCode();
}
memset(&odb, 0, sizeof(odb));
do
{
printf("Podaj numer portu nasluchu (0-65535): ");
scanf("%d",&port);
}
while (port<0 || port>65535);
odb.sin_addr.s_addr = htonl(INADDR_ANY);
odb.sin_family = AF_INET;
odb.sin_port = htons(port);
if ( (bind(odbiorca, (struct sockaddr*)&odb, sizeof(odb))) == SOCKET_ERROR)
{
printf("Nie mozna skonfigurowac gniazdka\n");
closesocket(odbiorca);
ErrorCode();
}
if(listen(odbiorca, SOMAXCONN) == SOCKET_ERROR)
{
printf("Nie mozna nasluchiwac\n");
closesocket(odbiorca);
ErrorCode();
}
printf("Oczekuje na polaczenie...\n");
nadawca = accept (odbiorca, (struct sockaddr*)&nad, &dl);
if(nadawca == INVALID_SOCKET)
{
printf ("Nieudana proba polaczenia!\n");
closesocket(nadawca);
closesocket(odbiorca);
ErrorCode();
}
else printf("Polaczenie nawiazane poprawnie\n");
recv(nadawca, bufor, 100, 0);
printf("Adres IP i port nadawcy: %s:%d\n", inet_ntoa(nad.sin_addr),htons(nad.sin_port));
printf("Odebrana wiadomosc: %s\n",bufor);
closesocket(nadawca);
closesocket(odbiorca);
}
void Nadawca()
{
SOCKET nadawca;
struct sockaddr_in nad;
int port;
struct hostent *host;
char ipodb[64], bufor[100];
nadawca = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(nadawca == INVALID_SOCKET)
{
printf("Nie mozna stworzyc gniazdka\n");
ErrorCode();
}
dane(&port, ipodb, bufor);
memset(&nad, 0, sizeof(nad));
nad.sin_family = AF_INET;
nad.sin_port = htons(port);
nad.sin_addr.s_addr = inet_addr(ipodb);
if (nad.sin_addr.s_addr == INADDR_NONE)
{
host = gethostbyname(ipodb);
if (host == NULL)
{
printf("Nie mozna skonwertowac adresu\n");
closesocket(nadawca);
ErrorCode();
}
memcpy(&nad.sin_addr, host->h_addr_list[0], host->h_length);
}
if (connect(nadawca, (struct sockaddr*)&nad, sizeof(nad)) == SOCKET_ERROR )
{
printf ("Nieudana proba polaczenia!\n");
closesocket(nadawca);
ErrorCode();
}
else printf("Polaczenie nawiazane poprawnie\nWiadomosc wyslana\n");
send(nadawca, bufor, strlen(bufor), 0);
closesocket(nadawca);
}
int main(void)
{
int wyb;
char name[32];
WSADATA wsaData;
WORD version;
version = MAKEWORD(2,0);
if (WSAStartup(version, &wsaData) != 0)
{
printf("Blad inicjalizacji WSA\n");
ErrorCode();
}
printf("Chcesz odebrac(1) czy wyslac(2) wiadomosc? ");
scanf("%d",&wyb);
if (wyb==1) Odbiorca();
if (wyb==2) Nadawca();
WSACleanup();
system("pause");
}