加勒比久久综合,国产精品伦一区二区,66精品视频在线观看,一区二区电影

合肥生活安徽新聞合肥交通合肥房產生活服務合肥教育合肥招聘合肥旅游文化藝術合肥美食合肥地圖合肥社保合肥醫院企業服務合肥法律

COEN 146代寫、代做TCP/IP Socket Programm

時間:2024-04-24  來源:合肥網hfw.cc  作者:hfw.cc 我要糾錯



COEN 146: Computer Networks
Lab 3: TCP/IP Socket Programming

Objectives
1.To develop client/ server applications using TCP/IP Sockets
2.To write a C program to transfer file over TCP/IP Socket

TCP/IP Client/ Server[ http://beej.us/guide/bgnet/] 
The following figure shows the steps taken by each program:
On the client and server sides:

The socket() system call creates an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets.

int sockfd = socket(domain, type, protocol)
●sockfd: socket descriptor, an integer (like a file-handle)
●domain: integer, communication domain e.g., AF_INET (IPv4 protocol) , AF_INET6 (IPv6 protocol), AF_UNIX (local channel, similar to pipes)
●type: communication type
SOCK_STREAM: TCP (reliable, connection oriented)
SOCK_DGRAM: UDP (unreliable, connectionless)
SOCK_RAW (direct IP service)
●protocol: This is useful in cases where some families may have more than one protocol to support a given type of service. Protocol value for Internet Protocol (IP), which is 0. This is the same number which appears on protocol field in the IP header of a packet.

#include <sys/socket.h>
...
...if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
{
    perror(“cannot create socket”); 
    return 0; 
}

On the server side:

After creation of the socket, bind() system call binds the socket to the address and port number specified in addr(custom data structure). In the example code, we bind the server to the localhost, hence we use INADDR_ANY to specify the IP address.

int bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen);
●addr: Points to a sockaddr structure containing the address to be bound to the socket. The length and format of the address depend on the address family of the socket.
●addrlen: Specifies the length of the sockaddr structure pointed to by the addr argument. 

The listen() function puts the server socket in a passive mode, where it waits for the client to approach the server to make a connection. 

int listen(int sockfd, int backlog);
●backlog: defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED.

The accept() system call extracts the first connection request on the queue of pending connections for the listening socket (sockfd), creates a new connected socket, and returns a new file descriptor referring to that socket. At this point, connection is established between client and server, and they are ready to transfer data.

int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

On the client side:

The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. Server’s address and port is specified in addr.

int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

Read and write over socket:

     bzero(buffer, 256);
     n = read(newsockfd, buffer, 255);
     if (n < 0) error("ERROR reading from socket");
     printf("Here is the message: %s\n", buffer);

This code initializes the buffer using the bzero() function, and then reads from the socket. 
Note that the read call uses the new file descriptor, the one returned by accept(), not the original file descriptor returned by socket(). 

Note also that the read() will block until there is something for it to read in the socket, i.e. after the client has executed a write(). It will read either the total number of characters in the socket or 255, whichever is less, and return the number of characters read


    n = write(newsockfd, "I got your message", 18);
         if (n < 0) error("ERROR writing to socket");

Once a connection has been established, both ends can both read and write to the connection. Naturally, everything written by the client will be read by the server, and everything written by the server will be read by the client. This code simply writes a short message to the client. The last argument of write is the size of the message.


Structures:

Address format
An IP socket address is defined as a combination of an IP interface address and a 16-bit port number. The basic IP protocol does not supply port numbers, they are implemented by higher level protocols like UDP and TCP. On raw sockets sin_port is set to the IP protocol.

struct sockaddr_in {
    sa_family_t    sin_family; /* address family: AF_INET */
    in_port_t      sin_port;   /* port in network byte order */
    struct in_addr sin_addr;   /* internet address */
};

/* Internet address. */
struct in_addr {
    uint**_t       s_addr;     /* address in network byte order */
};

This is defined in netinet/in.h

sin_family is always set to AF_INET. 

sin_port contains the port in network byte order. The port numbers below 1024 are called privileged ports (or sometimes: reserved ports). Only privileged processes) may bind to these sockets.

sin_addr is the IP host address. 

s_addr member of struct in_addr contains the host interface address in network byte order. 
in_addr should be assigned one of the INADDR_* values (e.g., INADDR_ANY) or set using the inet_aton, inet_addr, inet_makeaddr library functions or directly with the name resolver (see gethostbyname).

INADDR_ANY allows your program to work without knowing the IP address of the machine it was running on, or, in the case of a machine with multiple network interfaces, it allowed your server to receive packets destined to any of the interfaces. 

INADDR_ANY has the following semantics: When receiving, a socket bound to this address receives packets from all interfaces. For example, suppose that a host has interfaces 0, 1 and 2. If a UDP socket on this host is bound using INADDR_ANY and udp port 8000, then the socket will receive all packets for port 8000 that arrive on interfaces 0, 1, or 2. If a second socket attempts to Bind to port 8000 on interface 1, the Bind will fail since the first socket already “owns” that port/interface.
Example:
serv_addr.sin_addr.s_addr = htonl (INADDR_ANY);
●Note: "Network byte order" always means big endian. "Host byte order" depends on architecture of host. Depending on CPU, host byte order may be little endian, big endian or something else. 
●The htonl() function translates a long integer from host byte order to network byte order.

To bind socket with localhost, before you invoke the bind function, sin_addr.s_addr field of the sockaddr_in structure should be set properly. The proper value can be obtained either by 

my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1")
or by 
my_sockaddress.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
To convert an address in its standard text format into its numeric binary form use the inet_pton() function. The argument af specifies the family of the address. 

#define _OPEN_SYS_SOCK_IPV6
#include <arpa/inet.h>

int inet_pton(int af, const char *src, void *dst);

Recap - File transfer:
●Binary file: jpg, png, bmp, tiff etc.
●Text file: txt, html, xml, css, json etc.

You may use functions or system calls for file transfer. C Function connects the C code to file using I/O stream, while system call connects C code to file using file descriptor.
●File descriptor is integer that uniquely identifies an open file of the process.
●I/O stream sequence of bytes of data.

A Stream provides high level interface, while File descriptor provide a low-level interface. Streams are represented as FILE * object, while File descriptors are represented as objects of type int.

C Functions to open and close a binary/text file
fopen(): C Functions to open a binary/text file, defined as:
FILE *fopen(const char *file_name, const char *mode_of_operation);
where:
●file_name: file to open
●mode_of_operation: refers to the mode of the file access, For example:- r: read , w: write , a: append etc
●fopen() return a pointer to FILE if success, else NULL is returned
●fclose(): C Functions to close a binary/text file.

fclose(): C Functions to close a binary/text file, defined as:
fclose( FILE *file_name);
Where:
●file_name: file to close
●fclose () function returns zero on success, or EOF if there is an error

C Functions to read and write a binary file
fread(): C function to read binary file, defined as:
fread(void * ptr, size_t size, size_t count, FILE * stream);
where:
●ptr- it specifies the pointer to the block of memory with a size of at least (size*count) bytes to store the objects.
●size - it specifies the size of each objects in bytes.
●count: it specifies the number of elements, each one with a size of size bytes.
●stream - This is the pointer to a FILE object that specifies an input stream.
●Returns the number of items read


fwrite (): C function to write binary file, defined as:
fwrite (void *ptr, size_t size, size_t count, FILE *stream);
where:
●Returns number of items written
●*arguments of fwrite are similar to fread. Only difference is of read and write.

For example:
To open "lab3.dat" file in read mode then function would be:
FILE* demo; // demo is a pointer of type FILE
char buffer[100]; // block of memory (ptr)
demo= fopen("lab3.dat", "r"); // open lab3.dat in read mode
fread(&buffer, sizeof(buffer), 1, demo); // read 1 element of size = size of buffer (100)
fclose(demo); // close the file

C Functions to read and write the text file.
fscanf (): C function to read text file.
fscanf(FILE *ptr, const char *format, ...)
Where:
●Reads formatted input from the stream.
●Ptr: File from which data is read.
●format: format of data read.
●returns the number of input items successfully matched and assigned, zero if failure

fprintf(): C function to write a text file.
fprintf(FILE *ptr, const char *format, ...);
Where:
●*arguments similar to fscanf ()

For example:
FILE *demo; // demo is a pointer of type FILE
demo= FILE *fopen("lab3.dat", "r"); // open lab3.dat in read mode
/* Assuming that lab3.dat has content in below format
City
Population
….
*/
char buf[100]; // block of memory
fscanf(demo, "%s", buf); // to read a text file
fclose(demo); // close the file
*to read whole file use while loop


System Call to open, close, read and write a text/binary file.
open(): System call to open a binary/text file, defined as:
open (const char* Path, int flags [, int mode ]);
Where:
●returns file descriptor used on success and -1 upon failure
●Path :- path to file
●flags :- O_RDONLY: read only, O_WRONLY: write only, O_RDWR: read and write, O_CREAT: create file if it doesn’t exist, O_EXCL: prevent creation if it already exists


close(): System call to close a binary/text file, defined as:
close(int fd);
where:
●return 0 on success and -1 on error.
●fd : file descriptor which uniquely identifies an open file of the process

read(): System call to read a binary/text file.
read (int fd, void* buf, size_t len);
where:
●returns 0 on reaching end of file, -1 on error or on signal interrupt
●fd: file descriptor
●buf: buffer to read data from
●len: length of buffer

write(): System call to write a binary/text file.
write (int fd, void* buf, size_t len);
where:
●*arguments and return of write are similar to read(). 

For example:
int fd = open("lab3.dat", O_RDONLY | O_CREAT); //if file not in directory, file is 
created
Close(fd);


Implementation steps:
Step 1.[30%] Write a C program for a TCP server that accepts a client connection for file transfer. 
Step 2.[25%] Write a C program for a TCP client that connects to the server. In this case
a.The client connects to the server and request a file to download from the server. 
b.The server accepts the connection and transfers the file to the client

Step 3.Compile and run. Note: you may use the IP address 127.0.0.1 (loop back IP address) for a local host, i.e. both of your client and server run on the same machine. 

[20%] Demonstrate your program to the TA:
a.Your client and server on your same machine
b.Your client and your classmate’s server IP address. You may to discuss with the TA if you run into access problems  

Multiple Clients – Concurrent Server 
In general, a TCP server is designed as a concurrent server to server multiple clients. This means when a client sends a request for a file transfer, the sever accepts the connection request and spawns a thread to handle this transfer on the connection descriptor. The server will then continue in a loop listening for another client connection request to handle another file transfer.

Step 4.[20%] Write a C program for a concurrent TCP server that accepts and responds to multiple client connection requests, each requesting a file transfer. Modify your TCP server in Step 1 so that when the server accepts a connection from a client it spawns a separate thread to handle this specific client connection for file transfer. 

Note: You will have several threads (at the same time) running on the server transferring copies of src.dat files to clients that each will save at their destination as – dst.dat file (possibly needs to be numbered differently on the same host).

[5%] Demonstrate to the TA, multiple clients making file transfer request to the server and that the server makes multiple transfers at the same time. Make N = 5. Upload your source code to Camino. 

Note: To be able to see 5 threads handling connections at the same time, you may need to introduce a delay of a few second in the process of data transfer to make it visible. This is due to the fact completing thread file transfer takes a fraction of a millisecond if not a microsecond. 

Requirements to complete the lab
1.Demo to the TA correct execution of your programs [recall: a successful demo is 25% of the grade]
2.Submit the source code of your program as .c files on Camino

Please start each program with a descriptive block that includes minimally the following information:

請加QQ:99515681  郵箱:99515681@qq.com   WX:codinghelp















 

掃一掃在手機打開當前頁
  • 上一篇:SEHH2042代做、c/c++程序設計代寫
  • 下一篇:菲律賓落地簽回國(落地簽離境注意事項)
  • 無相關信息
    合肥生活資訊

    合肥圖文信息
    2025年10月份更新拼多多改銷助手小象助手多多出評軟件
    2025年10月份更新拼多多改銷助手小象助手多
    有限元分析 CAE仿真分析服務-企業/產品研發/客戶要求/設計優化
    有限元分析 CAE仿真分析服務-企業/產品研發
    急尋熱仿真分析?代做熱仿真服務+熱設計優化
    急尋熱仿真分析?代做熱仿真服務+熱設計優化
    出評 開團工具
    出評 開團工具
    挖掘機濾芯提升發動機性能
    挖掘機濾芯提升發動機性能
    海信羅馬假日洗衣機亮相AWE  復古美學與現代科技完美結合
    海信羅馬假日洗衣機亮相AWE 復古美學與現代
    合肥機場巴士4號線
    合肥機場巴士4號線
    合肥機場巴士3號線
    合肥機場巴士3號線
  • 短信驗證碼 目錄網 排行網

    關于我們 | 打賞支持 | 廣告服務 | 聯系我們 | 網站地圖 | 免責聲明 | 幫助中心 | 友情鏈接 |

    Copyright © 2025 hfw.cc Inc. All Rights Reserved. 合肥網 版權所有
    ICP備06013414號-3 公安備 42010502001045

    玖玖在线播放| 特黄特色欧美大片| 蜜桃视频www网站在线观看| 精品国内亚洲2022精品成人| 亚洲乱码视频| 国产在线观看www| 久久成人综合| 日韩 欧美一区二区三区| 99精品国自产在线| 国产农村妇女精品一二区| 91免费精品国偷自产在线在线| 欧美aaaaaa午夜精品| www.youjizz.com在线| 欧美~级网站不卡| 日本在线成人| 亚洲精品免费观看| 日韩一区二区三区在线免费观看| 欧美日韩国产探花| 福利电影一区| 亚洲精品一级二级三级| 国产一区二区三区久久| 日韩中文字幕高清在线观看| 黄色亚洲大片免费在线观看| 精品高清在线| 日韩成人dvd| 欧美日韩伊人| 一本综合精品| 日韩免费福利视频| 鲁鲁在线中文| 在线视频免费在线观看一区二区| 亚洲天堂久久| 成人毛片在线| 韩国女主播一区二区三区 | 欧美亚洲tv| 日韩av网站在线免费观看| 亚洲伦理久久| 亚洲人成毛片在线播放女女| 999国产精品亚洲77777| 五月天国产在线| 漫画在线观看av| 色777狠狠狠综合伊人| 西西人体一区二区| 在线亚洲伦理| 日韩一级在线| 欧美精品激情| 狠狠干综合网| 一本色道88久久加勒比精品| 五月综合激情| 羞羞答答成人影院www| 激情丁香综合| 91九色精品| 91久久亚洲| 亚洲激情不卡| 夜夜精品视频| 久热精品视频| 日本一本不卡| 水蜜桃在线视频| av综合电影网站| 日韩电影一区| 一本大道色婷婷在线| 天天免费亚洲黑人免费| 日韩大片在线观看| 亚洲精品tv| 日本一区二区中文字幕| 欧美一级二区| 亚洲精品影视| 亚洲人成网亚洲欧洲无码| 日韩欧美影院| 成人在线免费观看网站| 99久久婷婷| 欧美在线网站| 蜜臀久久久久久久| 综合日韩av| 99精品热6080yy久久| 国产精品v亚洲精品v日韩精品 | 视频一区欧美日韩| 欧美韩日一区| 亚洲四虎影院| 国产精品啊v在线| 亚洲精品小区久久久久久| 欧美视频一区| 欧美亚洲激情| 天堂av在线一区| 日韩欧美一区二区三区在线观看| 国产欧美日韩一级| 国产亚洲字幕| 欧美色图麻豆| 香蕉精品视频在线观看| 欧美aaaaaaaaaaaa| 欧美在线三区| 少妇一区二区视频| 秋霞欧美视频| 99热免费精品在线观看| 九色porny自拍视频在线观看| 国产69精品久久久久9999人| 亚洲天天综合| aaa国产精品| 欧美日韩国产高清| 国产v综合v| 成人在线精品| 国产精品香蕉| 美女尤物久久精品| 国产日韩一区二区三区在线播放| 国产一区二区三区四区五区| 久久99偷拍| 久久久久久婷| 麻豆国产91在线播放| 日韩电影在线一区二区| 五月天综合网站| 日韩免费在线| 国产一区二区三区探花| 久久美女性网| 国产va在线视频| av在线国产精品| 久久亚洲成人| 久草在线资源福利站| www.久久爱.com| 免费成人网www| 欧洲成人一区| 九九九九九九精品任你躁| 伊人色**天天综合婷婷| 福利精品一区| 91成人午夜| 亚洲一区图片| 亚洲巨乳在线| 国产99精品| 四虎4545www国产精品 | 国产精品嫩草99av在线| 国产日韩欧美一区二区三区在线观看| 日韩精品一区二区三区免费视频| 黄色亚洲在线| 日日欢夜夜爽一区| 欧美成人基地| 日韩免费福利视频| 我要色综合中文字幕| 免费在线观看不卡| 国产欧美激情| 亚洲一区二区伦理| 综合激情网站| 99国产精品久久久久久久成人热| 日韩一区二区三区四区五区| www.爱久久| 欧美a级在线观看| 婷婷精品在线| 蜜臀av一区二区在线免费观看| 伊人久久大香线蕉综合影院首页| 极品av少妇一区二区| 美女精品一区二区| 久久综合99| 麻豆国产91在线播放| 亚洲第一偷拍| 一区二区电影| 国产精品普通话对白| 一区二区三区无毛| 久久一二三区| 天海翼亚洲一区二区三区| 国产精品25p| 日韩电影在线免费看| 亚洲黄色网址| ccyy激情综合| 欧美a级在线观看| 亚洲va在线| 日韩高清在线观看| 99成人精品| 国产影视精品一区二区三区| 日韩av自拍| 丁香5月婷婷久久| 欧美另类激情| 好看的亚洲午夜视频在线| 国产欧美大片| av资源亚洲| 国产91精品对白在线播放| 国内精品嫩模av私拍在线观看| 9色国产精品| 日韩高清一区| 久久精品国产色蜜蜜麻豆| 欧美在线网站| 日韩av网站在线观看| 免费在线观看一区| 午夜日韩av| 精品欧美视频| 一区二区三区国产在线| 国产亚洲精品v| 麻豆国产一区| 久久精品国产亚洲一区二区三区| 最新成人av网站| 1204国产成人精品视频| 国产日韩一区| 免费在线观看日韩欧美| 精品视频高潮| 欧美极品一区二区三区| 亚洲国产福利| 性欧美欧美巨大69| 日韩一区二区三区在线看| 99精品视频免费| 国产精品99一区二区三| 亚洲天堂偷拍| 久久的色偷偷| 亚洲精品婷婷| 日韩毛片在线|