自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

TCP/IP網(wǎng)絡(luò)編程 套接字中標(biāo)準(zhǔn)I/O的使用

網(wǎng)絡(luò) 網(wǎng)絡(luò)管理
什么是標(biāo)準(zhǔn)I/O?其實是指C語言里的文件操作函數(shù),如:fopen,feof,fgetc,fputs等函數(shù),他們和平臺無關(guān)。

標(biāo)準(zhǔn)I/O

1,什么是標(biāo)準(zhǔn)I/O?其實是指C語言里的文件操作函數(shù),如:fopen,feof,fgetc,fputs等函數(shù),他們和平臺無關(guān)。

2,網(wǎng)絡(luò)通信中使用標(biāo)準(zhǔn)I/O的優(yōu)點:

良好的移植性。良好移植性這個不需多解釋,不僅是I/O函數(shù),所有的標(biāo)準(zhǔn)函數(shù)都具有良好的移植性。因為,為了支持所有的操作系統(tǒng)(編譯器),這些函數(shù)都是按照ANSI C標(biāo)準(zhǔn)定義的。

標(biāo)準(zhǔn)I/O函數(shù)可以利用緩沖提高性能。在網(wǎng)絡(luò)通信中,read,write傳輸數(shù)據(jù)只有一種套接字緩沖,但使用標(biāo)準(zhǔn)I/O傳輸會有額外的緩沖,即I/O緩沖和套接字緩沖兩個。使用I/O緩沖主要是為了提高性能,需要傳輸?shù)臄?shù)據(jù)越多時越明顯。因為,一次發(fā)送更多的數(shù)據(jù)要比分多次發(fā)送同樣的數(shù)據(jù)性能要高。發(fā)送一次數(shù)據(jù)就對應(yīng)一個數(shù)據(jù)包,往往數(shù)據(jù)包的頭信息比較大,它與數(shù)據(jù)大小無關(guān)。

3,網(wǎng)絡(luò)通信中使用標(biāo)準(zhǔn)I/O的缺點:

不容易進(jìn)行雙向通信。

有時可能頻繁調(diào)用fflush函數(shù)。

需要以FILE結(jié)構(gòu)體指針的形式返回文件描述符。

4,轉(zhuǎn)換函數(shù)

//將文件描述符轉(zhuǎn)換為標(biāo)準(zhǔn)I/O函數(shù)中使用的FILE結(jié)構(gòu)體指針

FILE * fdopen(int fildes, const char *mode);

成功時返回轉(zhuǎn)換的FILE結(jié)構(gòu)體指針,失敗返回NULL

//將FILE結(jié)構(gòu)體指針轉(zhuǎn)換為文件描述符

int fileno(FILE *stream);

成功返回轉(zhuǎn)換后的文件描述符,失敗返回-1

注釋:套接字中使用標(biāo)準(zhǔn)I/O,其實主要是運用在需要傳輸大量數(shù)據(jù)的情況,因為其需要編寫額外代碼,所以并不像想象中的那么常用。

基于套接字的標(biāo)準(zhǔn)I/O函數(shù)使用

//
// main.cpp
// hello_server
//
// Created by app05 on 15-9-9.
// Copyright (c) 2015年 app05. All rights reserved.
//
#include 
#include 
#include 
#include 
#include 
#include 
#define BUF_SIZE 1024
void error_handling(char *message);
int main(int argc, const char * argv[]) {
int serv_sock, clnt_sock;
char message[BUF_SIZE];
int str_len, i;
struct sockaddr_in serv_adr, clnt_adr;
socklen_t clnt_adr_sz;
FILE *readfp;
FILE *writefp;
if(argc != 2)
{
printf("Usage: %s \n", argv[0]);
exit(1);
}
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
error_handling("socket() error");
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_adr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr *) &serv_adr, sizeof(serv_adr)) == -1)
error_handling("bind() error");
if(listen(serv_sock, 5) == -1)
error_handling("listen() error");
clnt_adr_sz = sizeof(clnt_adr);
for (i = 0; i < 5; i++) {
clnt_sock = accept(serv_sock, (struct sockaddr *) &clnt_adr, &clnt_adr_sz);
if(clnt_sock == -1)
error_handling("accept() error");
else
printf("Connected client %d \n", i+1);
//將文件描述符轉(zhuǎn)換為FILE結(jié)構(gòu)體指針
readfp = fdopen(clnt_sock, "r");
writefp = fdopen(clnt_sock, "w");
while (!feof(readfp))
{
//轉(zhuǎn)化為標(biāo)準(zhǔn)I/O操作
fgets(message, BUF_SIZE, readfp);//相當(dāng)于read,接收
fputs(message, writefp); //相當(dāng)于write,發(fā)送
fflush(writefp); //刷新緩沖,立即顯示而不是一直放緩沖中,保證立即將數(shù)據(jù)傳輸?shù)娇头?
}
fclose(readfp);
fclose(writefp);
}
close(serv_sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}

#p#

//
// main.cpp
// hello_client
//
// Created by app05 on 15-9-9.
// Copyright (c) 2015年 app05. All rights reserved.
//
//
#include 
#include 
#include 
#include 
#include 
#include 
#define BUF_SIZE 1024
void error_handling(char *message);
int main(int argc, const char * argv[]) {
int sock;
char message[BUF_SIZE];
int str_len;
struct sockaddr_in serv_adr;
FILE *readfp;
FILE *writefp;
if(argc != 3)
{
printf("Usage: %s \n", argv[0]);
exit(1);
}
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock == -1)
error_handling("socket() error");
memset(&serv_adr, 0, sizeof(serv_adr));
serv_adr.sin_family = AF_INET;
serv_adr.sin_addr.s_addr = inet_addr(argv[1]);
serv_adr.sin_port = htons(atoi(argv[2]));
if (connect(sock, (struct sockaddr *) &serv_adr, sizeof(serv_adr)) == -1)
error_handling("connect() error");
else
puts("Connected ...............");
readfp = fdopen(sock, "r");
writefp = fdopen(sock, "w");
while (1)
{
fputs("Input message(Q to quit): ", stdout);
fgets(message, BUF_SIZE, stdin);
if (!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
break;
fputs(message, writefp);
fflush(writefp);
fgets(message, BUF_SIZE, readfp);
printf("Message from server : %s", message);
}
fclose(writefp);
fclose(readfp);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}

 [[152227]]

責(zé)任編輯:何妍 來源: CSDN博客
相關(guān)推薦

2015-10-19 09:34:42

TCPIP網(wǎng)絡(luò)協(xié)議

2014-12-11 09:20:30

TCP

2015-05-28 10:47:38

Unix網(wǎng)絡(luò)編程TCP

2012-01-06 13:58:47

JavaTCP

2021-02-05 15:20:06

網(wǎng)絡(luò)安全套接字命令

2014-12-17 09:22:10

網(wǎng)絡(luò)·安全技術(shù)周刊

2015-03-31 11:24:02

2019-09-18 20:07:06

AndroidTCP協(xié)議

2015-04-24 09:48:59

TCPsocketsocket編程

2021-03-19 11:32:20

TCPNVMeSSD

2015-10-21 10:24:05

TCPIP網(wǎng)絡(luò)協(xié)議

2009-03-10 13:59:41

C#套接字編程

2014-12-15 09:28:54

UDP

2013-12-27 13:39:23

Java套接字

2021-03-14 18:22:23

套接字網(wǎng)絡(luò)通信

2018-10-08 15:22:36

IO模型

2020-10-15 19:10:05

LinuxAPI函數(shù)

2010-05-11 13:36:50

Unix標(biāo)準(zhǔn)

2019-11-08 14:47:49

TCPIP網(wǎng)絡(luò)

2016-08-23 17:21:51

UnixLinux重定向
點贊
收藏

51CTO技術(shù)棧公眾號