(2)
; /* get our address info */
if (hp == NULL) /* we dont exist !? */
return(-1);
sa.sin_family= hp->h_addrtype; /* this is our host address */
sa.sin_port= htons(portnum); /* this is our port number */
if ((s= socket(AF_INET, SOCK_STREAM, 0)) /* obligatory includes */
#include
#include
#include
#include
#include
#include
#include
#include
#define PORTNUM 50000 /* random port number, we need something */
void fireman(void);
void do_something(int);
main()
{ int s, t;
if ((s= establish(PORTNUM)) 0)
;
}
/* this is the function that plays with the socket. it will be called
* after getting a connection.
*/
void do_something(int s)
{
/* do your thing with the socket here
:
:
*/
}
4、拨号 (如何调用 socket)
现在你应该知道如何建立 socket 来接受调用了。那么如何调用呢?和电话一样,你要先有个电话。用 socket() 函数来完成这件事情,就象建立侦听的 socket 一样。
在给 socket 地址后,你可以用 connect() 函数来连接侦听的 socket 了。下面是一段代码。
int call_socket(char *hostname, unsigned short portnum)
{ struct sockaddr_in sa;
struct hostent *hp;
int a, s;
if ((hp= gethostbyname(hostname)) == NULL) { /* do we know the hosts */
errno= ECONNREFUSED; /* address? */
return(-1); /* no */
}
memset(&sa,0,siz 作者: 凉心无悔 时间: 2005-5-14 21:11 标题: [转帖]SOCKET编程的基本概念