2. [代码c代码 client部分
001 | client.c |
002
003
#include <netinet/in.h> // for sockaddr_in
004
#include <sys/types.h> // for socket
005
#include <sys/socket.h> // for socket
006
#include <stdio.h> // for printf
007
#include <stdlib.h> // for exit
008
#include <string.h> // for bzero
009
/*
010
#include <sys/types.h>
011
#include <sys/stat.h>
012
#include <fcntl.h>
013
#include <unistd.h>
014
*/
015
016
#define HELLO_WORLD_SERVER_PORT 6666
017
#define BUFFER_SIZE 1024
018
#define FILE_NAME_MAX_SIZE 512
019
020
int
main(
int
argc,
char
**argv)
021
{
022
if
(argc != 2)
023
{
024
printf
(
"Usage: ./%s ServerIPAddress\n"
,argv[0]);
025
exit
(1);
026
}
027
028
//设置一个socket地址结构client_addr,代表客户机internet地址, 端口
029
struct
sockaddr_in client_addr;
030
bzero(&client_addr,
sizeof
(client_addr));
//把一段内存区的内容全部设置为0
031
client_addr.sin_family = AF_INET;
//internet协议族
032
client_addr.sin_addr.s_addr = htons(INADDR_ANY);
//INADDR_ANY表示自动获取本机地址
033
client_addr.sin_port = htons(0);
//0表示让系统自动分配一个空闲端口
034
//创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket
035
int
client_socket = socket(AF_INET,SOCK_STREAM,0);
036
if
( client_socket < 0)
037
{
038
printf
(
"Create Socket Failed!\n"
);
039
exit
(1);
040
}
041
//把客户机的socket和客户机的socket地址结构联系起来
042
if
( bind(client_socket,(
struct
sockaddr*)&client_addr,
sizeof
(client_addr)))
043
{
044
printf
(
"Client Bind Port Failed!\n"
);
045
exit
(1);
046
}
047
048
//设置一个socket地址结构server_addr,代表服务器的internet地址, 端口
049
struct
sockaddr_in server_addr;
050
bzero(&server_addr,
sizeof
(server_addr));
051
server_addr.sin_family = AF_INET;
052
if
(inet_aton(argv[1],&server_addr.sin_addr) == 0)
//服务器的IP地址来自程序的参数
053
{
054
printf
(
"Server IP Address Error!\n"
);
055
exit
(1);
056
}
057
server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT);
058
socklen_t server_addr_length =
sizeof
(server_addr);
059
//向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接
060
if
(connect(client_socket,(
struct
sockaddr*)&server_addr, server_addr_length) < 0)
061
{
062
printf
(
"Can Not Connect To %s!\n"
,argv[1]);
063
exit
(1);
064
}
065
066
char
file_name[FILE_NAME_MAX_SIZE+1];
067
bzero(file_name, FILE_NAME_MAX_SIZE+1);
068
printf
(
"Please Input File Name On Server:\t"
);
069
scanf
(
"%s"
, file_name);
070
071
char
buffer[BUFFER_SIZE];
072
bzero(buffer,BUFFER_SIZE);
073
strncpy
(buffer, file_name,
strlen
(file_name)>BUFFER_SIZE?BUFFER_SIZE:
strlen
(file_name));
074
//向服务器发送buffer中的数据
075
send(client_socket,buffer,BUFFER_SIZE,0);
076
077
// int fp = open(file_name, O_WRONLY|O_CREAT);
078
// if( fp < 0 )
079
FILE
* fp =
fopen
(file_name,
"w"
);
080
if
(NULL == fp )
081
{
082
printf
(
"File:\t%s Can Not Open To Write\n"
, file_name);
083
exit
(1);
084
}
085
086
//从服务器接收数据到buffer中
087
bzero(buffer,BUFFER_SIZE);
088
int
length = 0;
089
while
( length = recv(client_socket,buffer,BUFFER_SIZE,0))
090
{
091
if
(length < 0)
092
{
093
printf
(
"Recieve Data From Server %s Failed!\n"
, argv[1]);
094
break
;
095
}
096
// int write_length = write(fp, buffer,length);
097
int
write_length =
fwrite
(buffer,
sizeof
(
char
),length,fp);
098
if
(write_length<length)
099
{
100
printf
(
"File:\t%s Write Failed\n"
, file_name);
101
break
;
102
}
103
bzero(buffer,BUFFER_SIZE);
104
}
105
printf
(
"Recieve File:\t %s From Server[%s] Finished\n"
,file_name, argv[1]);
106
107
close(fp);
108
//关闭socket
109
close(client_socket);
110
return
0;
111
}
002 |
003 | #include <netinet/in.h> // for sockaddr_in |
004 | #include <sys/types.h> // for socket |
005 | #include <sys/socket.h> // for socket |
006 | #include <stdio.h> // for printf |
007 | #include <stdlib.h> // for exit |
008 | #include <string.h> // for bzero |
009 | /* |
010 | #include <sys/types.h> |
011 | #include <sys/stat.h> |
012 | #include <fcntl.h> |
013 | #include <unistd.h> |
014 | */ |
015 |
016 | #define HELLO_WORLD_SERVER_PORT 6666 |
017 | #define BUFFER_SIZE 1024 |
018 | #define FILE_NAME_MAX_SIZE 512 |
019 |
020 | int main( int argc, char **argv) |
021 | { |
022 | if (argc != 2) |
023 | { |
024 | printf ( "Usage: ./%s ServerIPAddress\n" ,argv[0]); |
025 | exit (1); |
026 | } |
027 |
028 | //设置一个socket地址结构client_addr,代表客户机internet地址, 端口 |
029 | struct sockaddr_in client_addr; |
030 | bzero(&client_addr, sizeof (client_addr)); //把一段内存区的内容全部设置为0 |
031 | client_addr.sin_family = AF_INET; //internet协议族 |
032 | client_addr.sin_addr.s_addr = htons(INADDR_ANY); //INADDR_ANY表示自动获取本机地址 |
033 | client_addr.sin_port = htons(0); //0表示让系统自动分配一个空闲端口 |
034 | //创建用于internet的流协议(TCP)socket,用client_socket代表客户机socket |
035 | int client_socket = socket(AF_INET,SOCK_STREAM,0); |
036 | if ( client_socket < 0) |
037 | { |
038 | printf ( "Create Socket Failed!\n" ); |
039 | exit (1); |
040 | } |
041 | //把客户机的socket和客户机的socket地址结构联系起来 |
042 | if ( bind(client_socket,( struct sockaddr*)&client_addr, sizeof (client_addr))) |
043 | { |
044 | printf ( "Client Bind Port Failed!\n" ); |
045 | exit (1); |
046 | } |
047 |
048 | //设置一个socket地址结构server_addr,代表服务器的internet地址, 端口 |
049 | struct sockaddr_in server_addr; |
050 | bzero(&server_addr, sizeof (server_addr)); |
051 | server_addr.sin_family = AF_INET; |
052 | if (inet_aton(argv[1],&server_addr.sin_addr) == 0) //服务器的IP地址来自程序的参数 |
053 | { |
054 | printf ( "Server IP Address Error!\n" ); |
055 | exit (1); |
056 | } |
057 | server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT); |
058 | socklen_t server_addr_length = sizeof (server_addr); |
059 | //向服务器发起连接,连接成功后client_socket代表了客户机和服务器的一个socket连接 |
060 | if (connect(client_socket,( struct sockaddr*)&server_addr, server_addr_length) < 0) |
061 | { |
062 | printf ( "Can Not Connect To %s!\n" ,argv[1]); |
063 | exit (1); |
064 | } |
065 |
066 | char file_name[FILE_NAME_MAX_SIZE+1]; |
067 | bzero(file_name, FILE_NAME_MAX_SIZE+1); |
068 | printf ( "Please Input File Name On Server:\t" ); |
069 | scanf ( "%s" , file_name); |
070 | |
071 | char buffer[BUFFER_SIZE]; |
072 | bzero(buffer,BUFFER_SIZE); |
073 | strncpy (buffer, file_name, strlen (file_name)>BUFFER_SIZE?BUFFER_SIZE: strlen (file_name)); |
074 | //向服务器发送buffer中的数据 |
075 | send(client_socket,buffer,BUFFER_SIZE,0); |
076 |
077 | // int fp = open(file_name, O_WRONLY|O_CREAT); |
078 | // if( fp < 0 ) |
079 | FILE * fp = fopen (file_name, "w" ); |
080 | if (NULL == fp ) |
081 | { |
082 | printf ( "File:\t%s Can Not Open To Write\n" , file_name); |
083 | exit (1); |
084 | } |
085 | |
086 | //从服务器接收数据到buffer中 |
087 | bzero(buffer,BUFFER_SIZE); |
088 | int length = 0; |
089 | while ( length = recv(client_socket,buffer,BUFFER_SIZE,0)) |
090 | { |
091 | if (length < 0) |
092 | { |
093 | printf ( "Recieve Data From Server %s Failed!\n" , argv[1]); |
094 | break ; |
095 | } |
096 | // int write_length = write(fp, buffer,length); |
097 | int write_length = fwrite (buffer, sizeof ( char ),length,fp); |
098 | if (write_length<length) |
099 | { |
100 | printf ( "File:\t%s Write Failed\n" , file_name); |
101 | break ; |
102 | } |
103 | bzero(buffer,BUFFER_SIZE); |
104 | } |
105 | printf ( "Recieve File:\t %s From Server[%s] Finished\n" ,file_name, argv[1]); |
106 | |
107 | close(fp); |
108 | //关闭socket |
109 | close(client_socket); |
110 | return 0; |
111 | } |