99爱在线视频这里只有精品_窝窝午夜看片成人精品_日韩精品久久久毛片一区二区_亚洲一区二区久久

合肥生活安徽新聞合肥交通合肥房產(chǎn)生活服務(wù)合肥教育合肥招聘合肥旅游文化藝術(shù)合肥美食合肥地圖合肥社保合肥醫(yī)院企業(yè)服務(wù)合肥法律

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

時(shí)間:2024-04-24  來源:合肥網(wǎng)hfw.cc  作者:hfw.cc 我要糾錯(cuò)



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















 

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

    合肥圖文信息
    2025年10月份更新拼多多改銷助手小象助手多多出評軟件
    2025年10月份更新拼多多改銷助手小象助手多
    有限元分析 CAE仿真分析服務(wù)-企業(yè)/產(chǎn)品研發(fā)/客戶要求/設(shè)計(jì)優(yōu)化
    有限元分析 CAE仿真分析服務(wù)-企業(yè)/產(chǎn)品研發(fā)
    急尋熱仿真分析?代做熱仿真服務(wù)+熱設(shè)計(jì)優(yōu)化
    急尋熱仿真分析?代做熱仿真服務(wù)+熱設(shè)計(jì)優(yōu)化
    出評 開團(tuán)工具
    出評 開團(tuán)工具
    挖掘機(jī)濾芯提升發(fā)動(dòng)機(jī)性能
    挖掘機(jī)濾芯提升發(fā)動(dòng)機(jī)性能
    海信羅馬假日洗衣機(jī)亮相AWE  復(fù)古美學(xué)與現(xiàn)代科技完美結(jié)合
    海信羅馬假日洗衣機(jī)亮相AWE 復(fù)古美學(xué)與現(xiàn)代
    合肥機(jī)場巴士4號線
    合肥機(jī)場巴士4號線
    合肥機(jī)場巴士3號線
    合肥機(jī)場巴士3號線
  • 短信驗(yàn)證碼 trae 豆包網(wǎng)頁版入口 目錄網(wǎng) 排行網(wǎng)

    關(guān)于我們 | 打賞支持 | 廣告服務(wù) | 聯(lián)系我們 | 網(wǎng)站地圖 | 免責(zé)聲明 | 幫助中心 | 友情鏈接 |

    Copyright © 2025 hfw.cc Inc. All Rights Reserved. 合肥網(wǎng) 版權(quán)所有
    ICP備06013414號-3 公安備 42010502001045

    99爱在线视频这里只有精品_窝窝午夜看片成人精品_日韩精品久久久毛片一区二区_亚洲一区二区久久

          9000px;">

                精品国一区二区三区| 一区二区高清视频在线观看| voyeur盗摄精品| 色拍拍在线精品视频8848| 亚洲日本成人在线观看| 亚洲成人激情综合网| 69久久夜色精品国产69蝌蚪网| 欧美一区二区三区不卡| 成人免费观看视频| 欧美日韩国产bt| 久久久影院官网| 亚洲午夜精品久久久久久久久| 精品亚洲成av人在线观看| 色婷婷综合久色| 精品美女一区二区三区| 亚洲精品乱码久久久久久黑人 | 精品夜夜嗨av一区二区三区| 不卡的电视剧免费网站有什么| 91精品国产91久久久久久最新毛片| 中文无字幕一区二区三区| 麻豆精品国产传媒mv男同| 欧美性欧美巨大黑白大战| 欧美激情一区不卡| 久久精品国产成人一区二区三区 | 亚洲成人av福利| 六月丁香婷婷久久| 色婷婷综合五月| 久久久不卡影院| 麻豆91在线观看| 91 com成人网| 亚洲成a人v欧美综合天堂下载| 97久久精品人人做人人爽50路| 国产亚洲成年网址在线观看| 激情成人综合网| 欧美一区二区三区爱爱| 日韩精品成人一区二区三区| 欧美日韩精品综合在线| 亚洲国产精品久久人人爱| 欧美性生活大片视频| 最新高清无码专区| 91首页免费视频| **欧美大码日韩| av动漫一区二区| 国产欧美一区二区精品忘忧草| 狠狠色丁香婷综合久久| 久久综合九色综合欧美亚洲| 激情文学综合网| 国产亚洲精品中文字幕| 国产精品亚洲人在线观看| 久久久久久久久久久久久夜| 国产白丝精品91爽爽久久| 久久精品一区八戒影视| 成人av在线看| 中文字幕乱码一区二区免费| 成人影视亚洲图片在线| 中文字幕日韩av资源站| 色呦呦国产精品| 亚洲高清免费观看| 88在线观看91蜜桃国自产| 日韩激情视频在线观看| 精品成人免费观看| 国产另类ts人妖一区二区| 中文字幕第一区综合| 色综合色狠狠天天综合色| 一区二区免费看| 欧美一区二区黄| 欧美精品一区二区三区高清aⅴ | 久久综合色婷婷| 国产伦精品一区二区三区在线观看| 精品日本一线二线三线不卡 | 欧美日韩的一区二区| 亚洲精品国产a久久久久久 | 欧美一区二区精品| 国产伦精品一区二区三区免费迷| 国产欧美日韩综合精品一区二区| 欧美色区777第一页| 婷婷成人激情在线网| 欧美xxxx老人做受| 99久久精品国产麻豆演员表| 亚洲一二三区在线观看| 国产老肥熟一区二区三区| 亚洲三级在线免费观看| 欧美精品第1页| 国产传媒日韩欧美成人| 亚洲一卡二卡三卡四卡五卡| 日韩精品一区二区三区老鸭窝| 国产传媒欧美日韩成人| 亚洲成国产人片在线观看| 精品国产露脸精彩对白| 91捆绑美女网站| 蜜臀av性久久久久蜜臀aⅴ四虎| 亚洲精品在线网站| 一本一道综合狠狠老| 久草精品在线观看| 亚洲欧美日韩在线不卡| 日韩午夜在线影院| 一本一道久久a久久精品综合蜜臀| 久久国产欧美日韩精品| 亚洲日本va午夜在线电影| 欧美成人午夜电影| 在线观看三级视频欧美| 奇米亚洲午夜久久精品| 亚洲免费资源在线播放| 国产亚洲综合在线| 在线观看一区日韩| 国产综合久久久久影院| 午夜a成v人精品| 亚洲国产精品一区二区尤物区| 中文字幕精品在线不卡| 欧美成人一区二区三区片免费| 欧美性猛交xxxxxx富婆| 成人黄色在线网站| 精品一区二区三区影院在线午夜| 午夜影院在线观看欧美| 亚洲欧美区自拍先锋| 国产精品网站在线| 国产精品一二三四五| 亚洲综合色丁香婷婷六月图片| 欧美激情一区在线| 久久毛片高清国产| 欧美日韩国产电影| 在线亚洲人成电影网站色www| 国产精品中文字幕日韩精品 | 久久精品人人做人人爽人人| 色偷偷久久一区二区三区| 韩国精品免费视频| 久久激五月天综合精品| 五月婷婷综合网| 一区二区三区日本| 国产精品情趣视频| 91精品国产乱码久久蜜臀| 欧美精品日日鲁夜夜添| 欧美视频在线观看一区| 91蝌蚪国产九色| 91伊人久久大香线蕉| 不卡高清视频专区| 91同城在线观看| 成人激情免费网站| 色香蕉成人二区免费| 99久久婷婷国产综合精品 | 欧美大白屁股肥臀xxxxxx| 在线视频一区二区三| 在线日韩av片| 欧美日韩一区二区三区四区五区| 懂色av噜噜一区二区三区av| 久久久久亚洲蜜桃| 国产精品色噜噜| 国产精品婷婷午夜在线观看| 国产精品久线在线观看| 亚洲天堂成人网| 亚洲与欧洲av电影| 亚洲国产成人av网| 蜜臀精品久久久久久蜜臀| 午夜影院在线观看欧美| 美女www一区二区| 经典三级在线一区| 9人人澡人人爽人人精品| 久久精品99久久久| 国产精品一区免费在线观看| 美女爽到高潮91| 成a人片亚洲日本久久| 日本高清不卡视频| 日韩欧美不卡在线观看视频| 国产精品美女久久久久久久久| 亚洲日本韩国一区| 免费高清视频精品| www.久久久久久久久| 欧美精品粉嫩高潮一区二区| 国产亚洲精品7777| 亚洲图片欧美色图| 黑人精品欧美一区二区蜜桃| 国产91丝袜在线播放0| 91啪九色porn原创视频在线观看| 欧美巨大另类极品videosbest| 欧美成人精品1314www| 中文字幕精品—区二区四季| 天天影视色香欲综合网老头| 国产成人亚洲精品狼色在线| 欧美亚洲自拍偷拍| 精品少妇一区二区三区免费观看| 中文字幕人成不卡一区| 美女在线一区二区| 91福利视频久久久久| 国产亚洲一本大道中文在线| 亚洲一区二区三区四区不卡| 精品中文字幕一区二区| 91国产丝袜在线播放| 中文字幕精品一区二区精品绿巨人| 日韩在线观看一区二区| 成人ar影院免费观看视频| 日韩欧美中文字幕一区| 亚洲午夜在线电影| 成人av在线影院| 国产日韩欧美激情| 精品一区在线看| 欧美麻豆精品久久久久久| 亚洲欧美欧美一区二区三区| 成人午夜免费视频| 精品国产123| 黑人精品欧美一区二区蜜桃|