|
发表于 2003-5-1 11:50:01
|
显示全部楼层
I just did one similar problem. hehe
I used TCP.
I remember UDP is for connectionless server & client.
There are some reference codes for UDP.
[code:1]
/*
* Program 10.10 - SERVER - Internet Domain - connectionless
*/
#include "local.h"
main( void ) {
int sock, n,
server_len, client_len;
struct sockaddr_in server, /* Address structures */
client;
/* create the SOCKET */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("SERVER socket ");
exit(1);
} /* set svr adr info */
server.sin_family = AF_INET; /* Address family */
server.sin_addr.s_addr = htonl(INADDR_ANY); /* use any address */
server.sin_port = htons(0); /* pick a free port */
/* BIND the socket */
if (bind(sock, (struct sockaddr *) &server, sizeof(server) ) < 0) {
perror("SERVER bind "); exit(2);
} /* obtain chosen adr */
if (getsockname(sock, (struct sockaddr *) &server, &server_len) < 0) {
perror("SERVER getsocketname "); exit(3);
} /* display port # */
printf("Server using port %d\n", ntohs(server.sin_port));
while ( 1 ) {
client_len = sizeof(client); /* estimate length */
memset(buf, 0, BUFSIZ); /* clear the buffer */
if ((n=recvfrom(sock, buf, BUFSIZ, 0, /* the clnt message */
(struct sockaddr *) &client, &client_len)) < 0){
perror("SERVER recvfrom ");
close(sock); exit(4);
}
write(fileno(stdout), buf, n); /* show msg to server */
memset(buf, 0, BUFSIZ); /* clear the buffer */
if (fgets(buf, BUFSIZ, stdin) != NULL ){ /* get server's msg */
if ((sendto(sock, buf, strlen(buf) ,0, /* send it to client */
(struct sockaddr *) &client, client_len)) <0){
perror("SERVER sendto ");
close(sock); exit(5);
}
}
}
}
[/code:1]
and
[code:1]
/* Program 10.11 - CLIENT - Internet Domain - connectionless */
#include "local.h"
main(int argc, char *argv[]){
int sock, n,
server_len;
static struct sockaddr_in /* Address structures */
server,
client;
struct hostent *host; /* For host info */
if ( argc < 3 ) { /* need server & port */
fprintf(stderr, "usage: %s server port_#\n", argv[0]);
exit(1);
}
if (!(host=gethostbyname(argv[1]))){ /* get server info */
perror("CLIENT gethostname "); exit(2);
} /* set svr adr info */
server.sin_family = AF_INET; /* address family */
memcpy(&server.sin_addr, host->h_addr, host->h_length); /* act adr */
server.sin_port = htons(atoi(argv[2])); /* @ passed in port # */
/* create a SOCKET */
if ((sock=socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("CLIENT socket "); exit(3);
} /* set clnt adr info */
client.sin_family = AF_INET; /* address family */
client.sin_addr.s_addr = htonl(INADDR_ANY); /* use any address */
client.sin_port = htons( 0 ); /* pick a free port */
/* BIND the socket */
if (bind(sock, (struct sockaddr *) &client, sizeof(client)) < 0) {
perror("CLIENT bind "); exit(4);
}
while( fgets(buf, BUFSIZ, stdin) != NULL ){ /* get clnt's msg */
server_len=sizeof(server); /* guess at length */
if (sendto( sock, buf, strlen(buf), 0, /* send msg to server */
(struct sockaddr *) &server, server_len) < 0 ){
perror("CLIENT sendto ");
close(sock); exit(5);
}
memset(buf,0,BUFSIZ); /* clear the buffer */
if ((n=recvfrom(sock, buf, BUFSIZ, 0, /* server's message */
(struct sockaddr *) &server, &server_len)) < 0){
perror("CLIENT recvfrom ");
close(sock); exit(6);
}
write(fileno(stdout),buf,n); /* show msg to clnt */
memset(buf,0,BUFSIZ); /* clear the buffer */
}
close(sock);
exit(0);
}
[/code:1]
The header file is
[code:1]
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 6969
static char buf[BUFSIZ]; /* Buffer for messages */
[/code:1] |
|