- #include <stdio.h>
- #include ‹stdlib.h>
- #include <string.h>
- #include ‹winsock2.h>
- #define BUF SIZE 30
- void ErrorHandling (char *message);int main(int arg, char *argv [])
{
WSADATA waData;
SOCKET servsock;
char message [BUF_SIZE];
int strLen;
int clntAdrSz;
SOCKADDR_IN servAdr, cIntAdr;
if(argc!=2) {
printf("Usage : % <port›\n", arg[0]);
exit (1);
}
if (WSAStartup (MAKEWORD(2, 2), &waData) |=0)
ErrorHandling("WSAStartup() error!");
servSock=socket (PF_INET, SOCK_DGRAM, 0);
if(servsock==INVALID_SOCKET)
ErrorHandling ("UDP socket creation error");
memset (&servAdr, 0, sizeof (servAdr)); servAdr.sin family=AF_ INET;
servAdr.sin_addr.s_addr=hton1(INADDR_ANY) ;
servAdr.sin_port=htons(atoi(argv [1]));
if(bind(servSock, (SOCKADDR*)&servAdr, sizeof(servAdr))==SOCKET_ERROR)
ErrorHandling("bind() error");
while(1)
{
cintAdrSz=sizeof(cintAdr);
strLen=recvfrom(servSock, message, BUF_SIZE, 0,
(SOCKADDR*)&cIntAdr, &cintAdrSz);
sendto(servSock, message, strLen, 0,
(SOCKADDR*)&cIntAdr, sizeof(cintAdr));
}
closesocket (servsock);
WSACleanup (); return 0;
}
void ErrorHandling(char *message)
{
fputs (message, stderr);
fputc("In', stderr);
exit (1);
}
。。。。。。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define BUF SIZE 30
void ErrorHandling(char *message);
int main(int arg, char *argv[])
{
WADATA waData;
SOCKET sock;
char message [BUF_SIZE];
int strLen;
SOCKADDR_IN servAdr;
if (argc!=3) {
printf("Usage : % <IP> ‹port›\n", arg[0]);
exit (1);
}
if(WSAStartup (MAKEWORD(2, 2), &wsaData) |=0)
ErrorHandling("WSAStartup() error!");
sock=socket (PF_INET, SOCK_DGRAM, 0);
if(sock==INVALID SOCKET)
ErrorHandling("socket () error");
memset (&servAdr, 0, sizeof(servAdr)) ; servAdr.sin_family=AF_INET;
servAdr. sin_addr.s_addr=inet_addr (argv[1]); servAdr.sin_port=htons(atoi(argv [2]));
connect (sock, (SOCKADDR*)&servAdr, sizeof(servAdr));
while(1)
{
fputs ("Insert message(q to quit): ", stdout); fgets (message, sizeof (message), stdin);
if(stremp(message, "q\n") I| !stremp(message, "Q\n"))
break;
send (sock, message, strlen (message), 0);
strLen=recv (sock, message, sizeof(message)-1, 0);
message strLene0;
printf ("Message from server: %", message);
}
closesocket (sock);
WSACleanup();
return 0;
}
void Errorandling(char *message)
{
fputs (message, stderr);
fputc('\n', stderr);
exit (1);
}