第7章 嵌入式uClinux及其应用开发(2)(6)

2019-05-18 21:41

{

int error = EXT_NO_ERROR;

error = ExtInit(CD);

if (error != EXT_NO_ERROR) goto EXIT_POINT; printf(\EXIT_POINT: return(error); } /* end ModeInit */

/* Function: ExtInit * Abstract:

* Called once at program startup to do any initialization. * A socket is created to listen for

* connection requests from the client. EXT_NO_ERROR is returned * on success, EXT_ERROR on failure. * NOTES:

* This function should not block. */

int ExtInit(ConnectData *UD) {

int sockStatus; struct sockaddr_in serverAddr;

int sFdAddSize = sizeof(struct sockaddr_in); int option = 1; int port = 17725;

int error = EXT_NO_ERROR; SOCKET sFd = INVALID_SOCKET; #ifdef WIN32 WSADATA data;

if (WSAStartup((MAKEWORD(1,1)),&data)) { fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; } #endif /*

* Create a TCP-based socket. */

memset((char *) &serverAddr,0,sFdAddSize); serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(port);

serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);

sFd = socket(AF_INET, SOCK_STREAM, 0); if (sFd == INVALID_SOCKET) { fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; } /*

* Listening socket should always use the SO_REUSEADDR option * (\ * Volume 1, 2nd edition, by W. Richard Stevens). */

sockStatus =

setsockopt(sFd,SOL_SOCKET,SO_REUSEADDR,(char*)&option,sizeof(option));

if (sockStatus == SOCK_ERR) {

fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; }

sockStatus =

bind(sFd, (struct sockaddr *) &serverAddr, sFdAddSize); if (sockStatus == SOCK_ERR) {

fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; }

sockStatus = listen(sFd, 1); if (sockStatus == SOCK_ERR) { fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; }

EXIT_POINT:

UD->msgFd = INVALID_SOCKET; UD->port=17725;

if (error == EXT_ERROR) {

if (sFd != INVALID_SOCKET) { close(sFd); }

UD->sFd = INVALID_SOCKET; } else {

UD->sFd = sFd; }

return(error); } /* end ExtInit */

int OpenConnection(ConnectData *UD) {

struct sockaddr_in clientAddr;

int sFdAddSize = sizeof(struct sockaddr_in);

int error = EXT_NO_ERROR; SOCKET msgFd = INVALID_SOCKET; const SOCKET sFd = UD->sFd; /*

* Wait to accept a connection on the message socket. */

msgFd = accept(sFd, (struct sockaddr *)&clientAddr, &sFdAddSize); if (msgFd == INVALID_SOCKET) {

fprintf(stderr,\ error = EXT_ERROR; goto EXIT_POINT; }

connectionMade = 1;


第7章 嵌入式uClinux及其应用开发(2)(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:分子克隆实验标准步骤

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: