linux下如何判断socket是否有效

linux下如何判断socket是否有效,第1张

增加对Linux socket连接 最近的一个项目的最大连接数是模拟多个套接字的客户端和服务器之间的通信。 Linux系统由于Linux的限制,/在include / linux / posix_types.h文件中有如下的宏定义:#UNDEF __FD_SETSIZE 的#define __FD_SETSIZE 1024 这个宏是当你需要超过1024个FD,如select()函数将监听错误定义的最大文件描述符1024。所以,你需要改变1024所需要的数量,但不超过65,535。但这是不够的仅仅。 第二步你需要的文件的进程打开的最大数量。具体的步骤是:1,CD /usr/src/linux-2.4/include/linux 2,六limits.h中编辑文件:的#define NR_OPEN 90240原1024 1024 的#define OPEN_MAX 10240原值 3值,六fs.h文件的#define INR_OPEN 10240原值1024 的#define NR_FILE 65536 8192原始值,内存64 / 1M比例计算的值,1G内存的计算公式为:64 * 10. 4 1024 的#define NR_RESERVED_FILES 128原值,CD /usr/src/linux-2.4/include/netBR>五,六tcp.h中的#define TCP_LHTABLE_SIZE的32 128原值易听听队列,建立大。 - 设为与内存相关的打开文件的最大数量,系统会减慢太多。 第三步是编译内核,具体步骤是:1.使清洁2.化妆3. make dep的4.做的bzImage 将导入的bzImage /启动重新启动系统! wc命令,以目前在建立套接字连接数统计| 与超过1024个客户端和服务器到服务器的终端使用netstat的连接。

客户端

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <time.h>

#include <errno.h>

#define SERVER_PORT_ID 6081

#define CLIENT_PORT_ID 6086

#define SERVER_HOST_ADDR "128.119.40.186"

/* gaia.cs.umass.edu */

#define MAXSIZE 512

#define ACK 2

#define NACK 3

#define REQUESTFILE 100

#define COMMANDNOTSUPPORTED 150

#define COMMANDSUPPORTED 160

#define BADFILENAME 200

#define FILENAMEOK 400

#define STARTTRANSFER 500

int readn(int sd,char *ptr,int size)

int writen(int sd,char *ptr,int size)

main(int argc,char *argv[])

{

int sockid, newsockid,i,getfile,ack,msg,msg_2,c,len

int no_writen,start_xfer, num_blks,num_last_blk

struct sockaddr_in my_addr, server_addr

FILE *fp

char in_buf[MAXSIZE]

if(argc != 2)

no_writen = 0

num_blks = 0

num_last_blk = 0

len = strlen(argv[1])

printf("client: creating socket\n")

if ((sockid = socket(AF_INET,SOCK_STREAM,0)) < 0)

{ printf("client: socket error : %d\n", errno) exit(0)

}

printf("client: binding my local socket\n")

bzero((char *) &my_addr,sizeof(my_addr))

my_addr.sin_family = AF_INET

my_addr.sin_addr.s_addr = htonl(INADDR_ANY)

my_addr.sin_port = htons(CLIENT_PORT_ID)

if (bind(sockid ,(struct sockaddr *) &my_addr,sizeof(my_addr)) < 0)

{printf("client: bind error :%d\n", errno) exit(0)

}

printf("client: starting connect\n")

bzero((char *) &server_addr,sizeof(server_addr))

server_addr.sin_family = AF_INET

server_addr.sin_addr.s_addr = inet_addr(SERVER_HOST_ADDR)

server_addr.sin_port = htons(SERVER_PORT_ID)

if (connect(sockid ,(struct sockaddr *) &server_addr,

sizeof(server_addr)) < 0)

{printf("client: connect error :%d\n", errno) exit(0)

}

/* Once we are here, we've got a connection to the server */

/* tell server that we want to get a file */

getfile = htons(REQUESTFILE)

printf("client: sending command request to ftp server\n")

if((writen(sockid,(char *)&getfile,sizeof(getfile))) < 0)

/* want for go-ahead from server */

msg = 0

if((readn(sockid,(char *)&msg,sizeof(msg)))< 0)

msg = ntohs(msg)

if (msg==COMMANDNOTSUPPORTED) {

printf("client: server refused command. goodbye\n")

exit(0)

}

else

printf("client: server replied %d, command supported\n",msg)

/* send file name to server */

printf("client: sending filename\n")

if ((writen(sockid,argv[1],len))< 0)

/* see if server replied that file name is OK */

msg_2 = 0

if ((readn(sockid,(char *)&msg_2,sizeof(msg_2)))< 0)

msg_2 = ntohs(msg_2)

if (msg_2 == BADFILENAME) {

printf("client: server reported bad file name. goodbye.\n")

exit(0)

}

else

printf("client: server replied %d, filename OK\n",msg_2)

/* CLIENT KNOWS SERVER HAS BEEN ABLE TO OPEN THE FILE IN READ

MODE AND IS ASKING FOR GO-AHEAD*/

/* CLIENT NOW OPENS A COPY OF THE FILE IN WRITE MODE AND SENDS

THE GOAHEAD TO SERVER*/

printf("client: sending start transfer command\n")

start_xfer = STARTTRANSFER

start_xfer = htons(start_xfer)

if ((writen(sockid,(char *)&start_xfer,sizeof(start_xfer)))< 0)

{printf("client: write error :%d\n", errno) exit(0)

}

if ((fp = fopen(argv[1],"w")) == NULL)

/*NOW THE CLIENT IS READING INFORMATION FROM THE SERVER REGARDING HOW MANY

FULL BLOCKS OF SIZE MAXSIZE IT CAN EXPECT. IT ALSO RECEIVES THE NUMBER

OF BYTES REMAINING IN THE LAST PARTIALLY FILLED BLOCK, IF ANY */

if((readn(sockid,(char *)&num_blks,sizeof(num_blks))) < 0)

num_blks = ntohs(num_blks)

printf("client: server responded: %d blocks in file\n",num_blks)

ack = ACK

ack = htons(ack)

if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)

{printf("client: ack write error :%d\n",errno)exit(0)

}

if((readn(sockid,(char *)&num_last_blk,sizeof(num_last_blk))) < 0)

num_last_blk = ntohs(num_last_blk)

printf("client: server responded: %d bytes last blk\n",num_last_blk)

if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)

{printf("client: ack write error :%d\n",errno)exit(0)

}

/* BEGIN READING BLOCKS BEING SENT BY SERVER */

printf("client: starting to get file contents\n")

for(i= 0 i < num_blks i ++) {

if((readn(sockid,in_buf,MAXSIZE)) < 0)

no_writen = fwrite(in_buf,sizeof(char),MAXSIZE,fp)

if (no_writen == 0)

if (no_writen != MAXSIZE)

/* send an ACK for this block */

if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)

printf(" %d...",i)

}

/*IF THERE IS A LAST PARTIALLY FILLED BLOCK, READ IT */

if (num_last_blk > 0) {

printf("%d\n",num_blks)

if((readn(sockid,in_buf,num_last_blk)) < 0)

no_writen = fwrite(in_buf,sizeof(char),num_last_blk,fp)

if (no_writen == 0)

if (no_writen != num_last_blk)

if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)

}

else printf("\n")

/*FILE TRANSFER ENDS. CLIENT TERMINATES AFTER CLOSING ALL ITS FILES

AND SOCKETS*/

fclose(fp)

printf("client: FILE TRANSFER COMPLETE\n")

close(sockid)

}

/* DUE TO THE FACT THAT BUFFER LIMITS IN KERNEL FOR THE SOCKET MAY BE

REACHED, IT IS POSSIBLE THAT READ AND WRITE MAY RETURN A POSITIVE VALUE

LESS THAN THE NUMBER REQUESTED. HENCE WE CALL THE TWO PROCEDURES

BELOW TO TAKE CARE OF SUCH EXIGENCIES */

int readn(int sd,char *ptr,int size)

{ int no_left,no_read

no_left = size

while (no_left > 0)

{ no_read = read(sd,ptr,no_left)

if(no_read <0) return(no_read)

if (no_read == 0) break

no_left -= no_read

ptr += no_read

}

return(size - no_left)

}

int writen(int sd,char *ptr,int size)

{ int no_left,no_written

no_left = size

while (no_left > 0)

{ no_written = write(sd,ptr,no_left)

if(no_written <=0) return(no_written)

no_left -= no_written

ptr += no_written

}

return(size - no_left)

}

服务端

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <time.h>

#include <errno.h>

#define MY_PORT_ID 6081

#define MAXLINE 256

#define MAXSIZE 512

#define ACK 2

#define NACK 3

#define REQUESTFILE 100

#define COMMANDNOTSUPPORTED 150

#define COMMANDSUPPORTED 160

#define BADFILENAME 200

#define FILENAMEOK 400

int writen(int sd,char *ptr,int size)

int readn(int sd,char *ptr,int size)

main() {

int sockid, newsd, pid, clilen

struct sockaddr_in my_addr, client_addr

printf("server: creating socket\n")

if ((sockid = socket(AF_INET,SOCK_STREAM,0)) < 0)

printf("server: binding my local socket\n")

bzero((char *) &my_addr,sizeof(my_addr))

my_addr.sin_family = AF_INET

my_addr.sin_port = htons(MY_PORT_ID)

my_addr.sin_addr.s_addr = htons(INADDR_ANY)

if (bind(sockid ,(struct sockaddr *) &my_addr,sizeof(my_addr)) < 0)

printf("server: starting listen \n")

if (listen(sockid,5) < 0)

while(1==1) {

/* ACCEPT A CONNECTION AND THEN CREATE A CHILD TO DO THE WORK */

/* LOOP BACK AND WAIT FOR ANOTHER CONNECTION */

printf("server: starting accept\n")

if ((newsd = accept(sockid ,(struct sockaddr *) &client_addr,

&clilen)) < 0)

printf("server: return from accept, socket for this ftp: %d\n",

newsd)

if ( (pid=fork()) == 0) {

/* CHILD PROC STARTS HERE. IT WILL DO ACTUAL FILE TRANSFER */

close(sockid) /* child shouldn't do an accept */

doftp(newsd)

close (newsd)

exit(0) /* child all done with work */

}

/* PARENT CONTINUES BELOW HERE */

close(newsd)/* parent all done with client, only child */

} /* will communicate with that client from now on */

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8032084.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存