雅思作文话题:年轻人

雅思作文话题:年轻人,第1张

为了方便广族困粗大考生更好的复习,新东方在线雅思频道为大家准备了有关雅思考试 作文 话题:年轻人的内容,以供各位考生考试复习参考,希望对考生复习有所帮助。

雅思作文话题:年轻人

写作题目

Some people think that young people should follow traditions of their society, and others think that young people should be free to behave as individuals.

Discuss both views and give your own opinion.

题目大致意思:到底年轻人(1)是应兆镇该遵守传统,(2)还是可以被允许有自由的行为?

隐形模板:起3承6转6合3

起--允许年轻人自由取舍传统,视传统为历史的导师,而不是未来的狱警。

承--(舍其劣)年轻人自由抗拒不好的传统束缚,如此社会才能进化。

转--(取其优)全力保卫尺橡优良的传统价值,如此相传社会的 文化 遗产。

合--年轻人在行为上分辨理性的活传统与非理性的死传统。

IELTS Example Essay

Every generation, young and old, should be allowed freedom to deal with the breaking down of certain patterns and standards being handed down from the past. As individuals, young people are particularly keen to the need to end whatever tradition they think is unreasonable for the society. All rebellious minds think alike--tradition is a guide for the past but not a jailer for the future.

Too often young men and women are enthusiastic about the evolution of beliefs and practices of their society, and their behaviors tend to challenge a number of elements of tradition. For that matter, mental independence is required because without an amount of freedom, tradition would be only an explanation for acting in conformity without thinking independently.

Accordingly, young people ought not to be forced to express their new feelings in a mummified form, otherwise the evolution in tradition could not happen. The point is that not just because something is traditional, so there is reason enough to do it. It is good, however, for young people to continue a worthwhile tradition, and it is also good not to be captive of an unreasonable tradition which is not worth having. Although social bondage persists, it makes a difference if young people are permitted to think and act in such a way as different from that of a conservative mind in which the word 'tradition' means a shortcut to thinking or an excuse for acting.

Probably a society can only successfully function with traditional values--diverse customs, thoughts, lifestyles, religion, music, dance, arts and languages, among other things of culture. Young people had better learn that modern traditions are built on the past just as the past was built on the times that went before it. Traditions bring continuity to one's existence in a society, even though this sort of continuity is precisely what has been increasingly lost through modernity.

Thus, it behooves young individuals to preserve their traditions of value by safeguarding against the negative influence from various factors such as industrialization, globalization, and assimilation or marginalization of special cultural groups. It certainly makes patriotic sense that young people's love for traditions worth continuing always strengthens the well-being of the society and never weakens all that is meant to identify themselves with their country. Good tradition being a good guide, young people, however rebellious, should not be entirely negative about the past with all its enduring heritages and necessary evils.

In conclusion, since a modern society is not supposed to be a dead community, young people may behave freely in treating tradition as the living faith of the dead and not as the dead faith of the living. They should not believe in traditions simply because many beliefs and practices have been handed down for many generations, but should instead observe and analyze anything that agrees with reason and is conductive to the good and benefit of the society. It takes a lot of history to make a tradition, so the best of which must be accepted and lived up to, from generation to generation.

雅思大作文 范文 :年轻人频繁换工作

2018年6月30日雅思写作大作文题目:An increasing number of people are changing their careers during their working time. What causes this trend and is it a positive or negative development for society?

6月30日的雅思大作文与本月7日的雅思大作文题目一样,主题都是年轻人频繁更换工作的原因及优劣势。这是一个大家都非常熟悉的话题,类似的话题曾经在2010年6月19日、2014年4月5日、2017年9月16日出现过。

在过去,更换一次工作是很难的,多数人都是在一个岗位上工作到退休,但现在刚 毕业 几年的年轻人如果没有换几次工作都会觉得自己吃亏了。那么,这到底是什么原因造成的呢?对于这种原因分析的题目,唐老师建议大家还是从三个层次(即物质层次、心理层次和社会层次)去思考,就容易把思路梳理得比较有条理。就年轻人更换工作而言

第一个理由肯定是为了挣取更高的工资,获得更好的发展机会

其次,从心理上讲,很多年轻人愿意去尝试新工作,认为这样可以结识不同领域的朋友,增加人生体验

最后,从社会角度看,由于服务行业的兴旺,电脑技术的发展,客观上也为年轻人频繁更换工作创造了可能性。

那么,频繁更换工作对年轻人来说是好还是坏呢?从好处上讲,这的确可以增加年轻人的工作经历,从而提高他们未来在就业市场上的竞争力,但也有坏处,比如无法在某个领域成为最顶级的专家,从而失去更多发展机会。

本题审题时,要专注两个关键词,即“年轻人”和“频繁更换工作”。以下是本题高分范文。

开头:

Not long ago, most people would choose to stay on the same job for the entire life, but today young people prefer to try different things and hop from one job to another.

不久以前,大多数人都会选择终生从事同一份工作,但今天的年轻人更喜欢尝试不同的事情,从一份工作跳到另一份工作。

解析:

(1) 本段直接进入话题,用过去和现在的对比来提出下文讨论的现象:年轻人不停更换工作。

(2) stay on the same job 坚持做同一份工作

(3) hop from one job to another 从一份工作跳到另一份工作

主体:

In my view, there mainly are three reasons why young people are doing so. At the most basic level, young people find that frequent job changing can often bring them the pay rise they expect. My friend Edward is a perfect example. As a program designer, he has worked for three different companies in the past five years, and now he earns almost five times as much as he did in the first company. This would not have been possible if he had not been mobile enough. The second reason is that by doing different jobs or careers, young people can increase their work experience and eventually become more versatile. This may give them a competitive edge, especially in today’s job market where an all-round employee is always preferred. Finally, changing jobs frequently nowadays has been made possible by the booming service industry and rapidly developing information technology and transport.

在我看来,年轻人之所以这样做,主要有三个原因。在最基本的层面上,年轻人发现频繁换工作往往会给他们带来期望的加薪。我的朋友爱德华就是个完美的例子。作为一名程序设计师,他在过去五年里为三家不同的公司工作过,现在他的收入几乎是第一家公司的五倍。如果他不是如此频繁地变动,那是不可能的。第二个原因是,通过做不同的工作或职业,年轻人可以增加他们的工作 经验 ,并最终变得更多才多艺。这可能会给他们一个竞争优势,特别是在当今的就业市场上,一个全面的员工总是被青睐的。最后,由于蓬勃发展的服务业和迅速发展的信息技术和运输,如今频繁更换工作已经成为可能。

解析

(1) 本段论述年轻人不停更换工作的三个原因,分别为物质原因(能挣更高工资,举例论证)、心理原因(增加工作经验+结果分析),社会原因(服务业、信息技术和交通的发展)。

(2) at the most basic level 在最基本的层面上

(3) versatile 全能的

(4) give sb. a competitive edge 给某人竞争优势

(5) booming 蓬勃发展的

主体:

Needless to say, job-hopping may be a bad thing if it is not well-considered. Shifting from job to job may prove to be a waste of time and opportunity since this may easily make a person Jack of all trades and master of none. As we know, the person who can provide the most professional service for the company enjoys the best opportunity of promotion. However, if a person does not find the job he is doing interesting or fulfilling, then his change job could be a win-win strategy both for the companies and employees. For the companies, “new bloods” often mean stronger labor force and vitality of the staff. For the employees, a new job means a new opportunity.

不用说,跳槽可能是一件坏事,如果没有没有好好考虑的话。从一份工作转到另一份工作可能是浪费时间和机会,因为这可能很容易使一个人成为万金油,但一无所专。我们知道,能够为公司提供最专业服务的人享有最佳的晋升机会。但是,如果一个人发现他所从事工作没有意义或没有成就感,那么他换工作对公司和员工来说都是双赢的策略。对公司来说,“新血液”通常意味着更强大的劳动力和活力。对员工来说,新的工作意味着新的机会。

解析

(1)本段讨论改换工作的坏处(无法专门做好一件事情)和好处(摆脱自己不喜欢的工作)。

(2)jack of all trades and master of none 什么都会但什么都不专的人

(3)fulfilling 有成就感的

结尾:

In light of the above discussion, I would conclude that no change of jobs is wasteful as long as it is carefully considered. Holding on to a job that we do not like is a crime against our precious life.

根据以上的讨论,我认为只要仔细考虑,换工作是不会浪费的。坚持一份我们不喜欢的工作是对我们宝贵生命的一种犯罪。

解析

(1)本段是结论,明确提出自己的观点。

(2)in light of 依据

(3)hold on to... 牢牢抱住坚持

雅思大作文真题 | 年轻人适合在政府担任要职吗?

雅思大作文题目

Some people think young people are not suitable for important positions in the government, while other people think it is a good idea for young people to take on these positions. Discuss both views and give your own opinion.

本周写作的话题是:年轻人是否适合在政府担任要职?

有些人认为是可以的,有些人认为不可以,题目要求我们讨论双方观点,并给出你自己的观点。本题是2010年7月10日的原题再现(是不是太久远了?),另外,2015年1月1日的考题(年轻人是否适合在机构担任领导?)也与本题非常接近。

年轻人是否适合在政府担任要职呢?这个题目当然应该关注“年轻人”、“政府”、“要职”这些关键词,应该讨论政府要职有哪些要求,年轻人有哪些特点,这些特点是否适合他们在政府中发挥作用,等等。注意我们在思维的时候不要过于复杂,要尽量简洁直接。比如政府要职的要求,如果要复杂地思考那就要考虑很多东西了(比如管理能力,正直,处理人际关系,素质等等)。我们要从众多思维中选择与年轻人相关的东西来讨论。

担任政府要职需要经验,这是年轻人缺乏的,因此年轻人不适合担任【反对】。

担任政府要职需要改变精神,这是年轻人擅长的,因此年轻人适合担任【赞同】。

我的观点:有些政府要职更注重经验,最好不要年轻人担任,但另一些要职更需要激情,年轻人完全可以担任【一分为二的结论】。

        老师笔记

01

As we can see on television, the important positions in the government are often held by gray-haired menand women. While it is assumed by many that the elderly people are more experienced and capable, I would say that some important government positions should also be open to young people.

我们可以在电视上看到,政府重要职位通常是由头发花白的男人和女人担任。虽然很多人认为,老年人更富有经验和能力,但我想说,政府的一些重要职位也应开放给年轻人。

解析

(1)本段为引入段,提出现象和自己的观点。

(2) gray-haired man 头发花白的人

(3) assume 假设,认为

(4) capable 有能力的

02

Undoubtedly, experience is of utmost importancefor many government positions. These positions have to deal with a great number of people every day and crisis may emergeany time. For example, a foreign affairs minister has to make quick responsesto the problems that may come from all over the world every minute. Without the rich experience collected from his long-time service in this field, it would be unlikely for him to fulfill his duties. Young people, therefore, are not really suitable for this type of government positions.

毫无疑问,对许多政府职位来说,经验是最重要的。这些职位每天必须与大量的人打交道,危机随时可能出现。例如,外交部长必须对世界各地每分钟可能出现的问题迅速做出反应。没有他在这一领域长期服务所积累的丰富经验,他不可能履行他的职责。因此,年轻人真不太适合这种政府职位。

解析

(1)本段论述某些政府职位需要经验,不适合年轻人,用外交部来举例。

(2) of utmost importance 极其重要 (= extremely important)

(3) emerge 出现

(4) response 反应

03

However, young people should be given the opportunity to hold those government positions which call formore passion than experience. Young people who are more open-minded and receptiveto new idea than the elderly, are more likely to solve problems in an innovative way, and to put theiroriginalideas into action, which may lead to unexpected benefits. As far as I understand, the national ministry of education as well as the primary schools and middle schools should be led by young people who are more inspiringand innovating.

然而,应该给年轻人机会担任那些需要更多激情而非经验的政府职位。年轻人比老年人更开放,更接受新想法,因此更有可能以创新的方式解决问题,并把自己新颖的想法付诸行动,这可能会带来意想不到的好处。在我看来,国家部 教育 以及中小学应该由更具启发性和创新性的年轻人来领导。

解析

(1)本段论述某些政府职位(比如教育部和中小学)应该由年轻人来担任。

(2) call for 要求

(3) receptive 包容的

(4) innovative 革新的

(5) original 新颖的

(6) inspiring 启发性的

04

It is, therefore, difficult to draw a general conclusion about whether young people should hold important government positions. For some positions which are more experience-based, the elderly people are more suitable candidates, yet for other positions which require more passionate spirit than experience, young people can perhaps do a better job than their elderly counterparts.

因此,很难对年轻人是否应该担任重要政府职务做普遍结论。对于一些更基于经验的职位,老年人是更合适的人选,但对于其他需要更多的激情而不是经验的职位,年轻人或许可以比他们的老年同行做得跟好。

解析

(1)本段提出自己的观点:有些职位需要老年人担任,有些职位需要年轻人担任。

(2) candidate 候选人

(3) ounterpart 相对应的人(物)

雅思TASK2大作文模拟题:年轻人问题

要求:

Young people who are still at school often feel just as much stress as working adults. What are some of the causes of this stress, and how does it affect individual students?

雅思作文话题:年轻人相关 文章 :

★ 雅思写作:高能十大话题题库

★ 雅思作文话题范文分析:反社会行为

★ 雅思写作大作文话题分类汇总

★ 雅思作文话题范文及分析:多元文化

★ 雅思作文范文及分析:多陪家人

★ 雅思写作生活类话题范文汇总

★ 2019雅思写作话题题库总结

★ 雅思写作话题解析:父母和孩子

★ 雅思写作、口语常考话题:“老龄化”相关词汇

★ 雅思写作讨论类话题写作技巧

我吵基用C++写的,可以调试的!要加分啊啊!记得啊!呵呵...

代码如下:

//功能:利用SOCKET传送文件的代码

/*server.h*/

#pragma comment(lib, "WS2_32")

#include

#include

//#include

#include

#ifndef COMMONDEF_H

#define COMMONDEF_H

#define MAX_PACKET_SIZE 10240 // 数据包的最大长度,单位是sizeof(char)

#define MAXFILEDIRLENGTH 256 // 存放文件路径的最大长度

#define PORT 4096 // 端口号升族谨

#define SERVER_IP "127.0.0.1" // server端的IP地址

// 各种消息的宏定义

#define INVALID_MSG -1 // 无效的消息标识

#define MSG_FILENAME 1 // 文件的名称

#define MSG_FILELENGTH 2 // 传送文件的长度

#define MSG_CLIENT_READY 3 // 客户端准备接收文件

#define MSG_FILE 4 // 传送文件

#define MSG_SENDFILESUCCESS 5 // 传送文件成功

#define MSG_OPENFILE_ERROR 10 // 打开文件失败,可能是文件路径错误找不到文件等原因

#define MSG_FILEALREADYEXIT_ERROR 11 // 要保存的文件已经存在了

class CCSDef

{

public:

#pragma pack(1) // 使结构体的数据按照1字节来对齐,省空间

// 消息头

struct TMSG_HEADER

{

char cMsgID// 消息标识

TMSG_HEADER(char MsgID = INVALID_MSG)

: cMsgID(MsgID)

{

}

}

// 请求传送的文件名

// 客户端传给服务器端的是全路径名称

// 服务器传穗搜回给客户端的是文件名

struct TMSG_FILENAME : public TMSG_HEADER

{

char szFileName[256]// 保存文件名的字符数组

TMSG_FILENAME()

: TMSG_HEADER(MSG_FILENAME)

{

}

}

// 传送文件长度

struct TMSG_FILELENGTH : public TMSG_HEADER

{

long lLength

TMSG_FILELENGTH(long length)

: TMSG_HEADER(MSG_FILELENGTH), lLength(length)

{

}

}

// Client端已经准备好了,要求Server端开始传送文件

struct TMSG_CLIENT_READY : public TMSG_HEADER

{

TMSG_CLIENT_READY()

: TMSG_HEADER(MSG_CLIENT_READY)

{

}

}

// 传送文件

struct TMSG_FILE : public TMSG_HEADER

{

union // 采用union保证了数据包的大小不大于MAX_PACKET_SIZE * sizeof(char)

{

char szBuff[MAX_PACKET_SIZE]

struct

{

int nStart

int nSize

char szBuff[MAX_PACKET_SIZE - 2 * sizeof(int)]

}tFile

}

TMSG_FILE()

: TMSG_HEADER(MSG_FILE)

{

}

}

// 传送文件成功

struct TMSG_SENDFILESUCCESS : public TMSG_HEADER

{

TMSG_SENDFILESUCCESS()

: TMSG_HEADER(MSG_SENDFILESUCCESS)

{

}

}

// 传送出错信息,包括:

// MSG_OPENFILE_ERROR:打开文件失败

// MSG_FILEALREADYEXIT_ERROR:要保存的文件已经存在了

struct TMSG_ERROR_MSG : public TMSG_HEADER

{

TMSG_ERROR_MSG(char cErrorMsg)

: TMSG_HEADER(cErrorMsg)

{

}

}

#pragma pack()

}

#endif

/*server.h*/

#pragma comment(lib, "WS2_32")

#include

#include

//#include

#include

#ifndef COMMONDEF_H

#define COMMONDEF_H

#define MAX_PACKET_SIZE 10240 // 数据包的最大长度,单位是sizeof(char)

#define MAXFILEDIRLENGTH 256 // 存放文件路径的最大长度

#define PORT 4096 // 端口号

#define SERVER_IP "127.0.0.1" // server端的IP地址

// 各种消息的宏定义

#define INVALID_MSG -1 // 无效的消息标识

#define MSG_FILENAME 1 // 文件的名称

#define MSG_FILELENGTH 2 // 传送文件的长度

#define MSG_CLIENT_READY 3 // 客户端准备接收文件

#define MSG_FILE 4 // 传送文件

#define MSG_SENDFILESUCCESS 5 // 传送文件成功

#define MSG_OPENFILE_ERROR 10 // 打开文件失败,可能是文件路径错误找不到文件等原因

#define MSG_FILEALREADYEXIT_ERROR 11 // 要保存的文件已经存在了

class CCSDef

{

public:

#pragma pack(1) // 使结构体的数据按照1字节来对齐,省空间

// 消息头

struct TMSG_HEADER

{

char cMsgID// 消息标识

TMSG_HEADER(char MsgID = INVALID_MSG)

: cMsgID(MsgID)

{

}

}

// 请求传送的文件名

// 客户端传给服务器端的是全路径名称

// 服务器传回给客户端的是文件名

struct TMSG_FILENAME : public TMSG_HEADER

{

char szFileName[256]// 保存文件名的字符数组

TMSG_FILENAME()

: TMSG_HEADER(MSG_FILENAME)

{

}

}

// 传送文件长度

struct TMSG_FILELENGTH : public TMSG_HEADER

{

long lLength

TMSG_FILELENGTH(long length)

: TMSG_HEADER(MSG_FILELENGTH), lLength(length)

{

}

}

// Client端已经准备好了,要求Server端开始传送文件

struct TMSG_CLIENT_READY : public TMSG_HEADER

{

TMSG_CLIENT_READY()

: TMSG_HEADER(MSG_CLIENT_READY)

{

}

}

// 传送文件

struct TMSG_FILE : public TMSG_HEADER

{

union // 采用union保证了数据包的大小不大于MAX_PACKET_SIZE * sizeof(char)

{

char szBuff[MAX_PACKET_SIZE]

struct

{

int nStart

int nSize

char szBuff[MAX_PACKET_SIZE - 2 * sizeof(int)]

}tFile

}

TMSG_FILE()

: TMSG_HEADER(MSG_FILE)

{

}

}

// 传送文件成功

struct TMSG_SENDFILESUCCESS : public TMSG_HEADER

{

TMSG_SENDFILESUCCESS()

: TMSG_HEADER(MSG_SENDFILESUCCESS)

{

}

}

// 传送出错信息,包括:

// MSG_OPENFILE_ERROR:打开文件失败

// MSG_FILEALREADYEXIT_ERROR:要保存的文件已经存在了

struct TMSG_ERROR_MSG : public TMSG_HEADER

{

TMSG_ERROR_MSG(char cErrorMsg)

: TMSG_HEADER(cErrorMsg)

{

}

}

#pragma pack()

}

#endif

/*server.cpp*/

#include "server.h"

char g_szNewFileName[MAXFILEDIRLENGTH]

char g_szBuff[MAX_PACKET_SIZE + 1]

long g_lLength

char* g_pBuff = NULL

// 初始化socket库

bool InitSocket()

// 关闭socket库

bool CloseSocket()

// 解析消息进行相应的处理

bool ProcessMsg(SOCKET sClient)

// 监听Client的消息

void ListenToClient()

// 打开文件

bool OpenFile(CCSDef::TMSG_HEADER* pMsgHeader, SOCKET sClient)

// 传送文件

bool SendFile(SOCKET sClient)

// 读取文件进入缓冲区

bool ReadFile(SOCKET sClient)

int main()

{

InitSocket()

ListenToClient()

CloseSocket()

return 0

}

void ListenToClient()

{

// 创建socket套接字

SOCKET sListen = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)

if (SOCKET_ERROR == sListen)

{

printf("Init Socket Error!\n")

return

}

// 绑定socket到一个本地地址

sockaddr_in sin

sin.sin_family = AF_INET

sin.sin_port = htons(PORT)

sin.sin_addr.S_un.S_addr = INADDR_ANY

if (::bind(sListen, (LPSOCKADDR)&sin, sizeof(sockaddr_in)) == SOCKET_ERROR)

{

printf("Bind Error!\n")

return

}

// 设置socket进入监听状态

if (::listen(sListen, 10) == SOCKET_ERROR)

{

printf("Listen Error!\n")

return

}

printf("Listening To Client...\n")

// 循环接收client端的连接请求

sockaddr_in ClientAddr

int nAddrLen = sizeof(sockaddr_in)

SOCKET sClient

while (INVALID_SOCKET == (sClient = ::accept(sListen, (sockaddr*)&ClientAddr, &nAddrLen)))

{

}

while (true == ProcessMsg(sClient))

{

}

// 关闭同客户端的连接

::closesocket(sClient)

::closesocket(sListen)

}

bool InitSocket()

{

// 初始化socket dll

WSADATA wsaData

WORD socketVersion = MAKEWORD(2, 2)

if (::WSAStartup(socketVersion, &wsaData) != 0)

{

printf("Init socket dll error\n")

return false

}

return true

}

bool CloseSocket()

{

// 释放winsock库

::WSACleanup()

if (NULL != g_pBuff)

{

delete [] g_pBuff

g_pBuff = NULL

}

return true

}

bool ProcessMsg(SOCKET sClient)

{

int nRecv = ::recv(sClient, g_szBuff, MAX_PACKET_SIZE + 1, 0)

if (nRecv >0)

{

g_szBuff[nRecv] = '\0'

}

// 解析命令

CCSDef::TMSG_HEADER* pMsgHeader = (CCSDef::TMSG_HEADER*)g_szBuff

switch (pMsgHeader->cMsgID)

{

case MSG_FILENAME: // 文件名

{

OpenFile(pMsgHeader, sClient)

}

break

case MSG_CLIENT_READY: // 客户端准备好了,开始传送文件

{

SendFile(sClient)

}

break

case MSG_SENDFILESUCCESS: // 传送文件成功

{

printf("Send File Success!\n")

return false

}

break

case MSG_FILEALREADYEXIT_ERROR: // 要保存的文件已经存在了

{

printf("The file reay to send already exit!\n")

return false

}

break

}

return true

}

bool ReadFile(SOCKET sClient)

{

if (NULL != g_pBuff)

{

return true

}

// 打开文件

FILE *pFile

if (NULL == (pFile = fopen(g_szNewFileName, "rb"))) // 打开文件失败

{

printf("Cannot find the file, request the client input file name again\n")

CCSDef::TMSG_ERROR_MSG tMsgErrorMsg(MSG_OPENFILE_ERROR)

::send(sClient, (char*)(&tMsgErrorMsg), sizeof(CCSDef::TMSG_ERROR_MSG), 0)

return false

}

// 把文件的长度传回到client去

fseek(pFile, 0, SEEK_END)

g_lLength = ftell(pFile)

printf("File Length = %d\n", g_lLength)

CCSDef::TMSG_FILELENGTH tMsgFileLength(g_lLength)

::send(sClient, (char*)(&tMsgFileLength), sizeof(CCSDef::TMSG_FILELENGTH), 0)

// 处理文件全路径名,把文件名分解出来

char szDrive[_MAX_DRIVE], szDir[_MAX_DIR], szFname[_MAX_FNAME], szExt[_MAX_EXT]

_splitpath(g_szNewFileName, szDrive, szDir, szFname, szExt)

strcat(szFname,szExt)

CCSDef::TMSG_FILENAME tMsgFileName

strcpy(tMsgFileName.szFileName, szFname)

printf("Send File Name: %s\n", tMsgFileName.szFileName)

::send(sClient, (char*)(&tMsgFileName), sizeof(CCSDef::TMSG_FILENAME), 0)

// 分配缓冲区读取文件内容

g_pBuff = new char[g_lLength + 1]

if (NULL == g_pBuff)

{

return false

}

fseek(pFile, 0, SEEK_SET)

fread(g_pBuff, sizeof(char), g_lLength, pFile)

g_pBuff[g_lLength] = '\0'

fclose(pFile)

return true

}

// 打开文件

bool OpenFile(CCSDef::TMSG_HEADER* pMsgHeader, SOCKET sClient)

{

CCSDef::TMSG_FILENAME* pRequestFilenameMsg = (CCSDef::TMSG_FILENAME*)pMsgHeader

// 对文件路径名进行一些处理

char *p1, *p2

for (p1 = pRequestFilenameMsg->szFileName, p2 = g_szNewFileName

'\0' != *p1

++p1, ++p2)

{

if ('\n' != *p1)

{

*p2 = *p1

}

if ('\\' == *p2)

{

*(++p2) = '\\'

}

}

*p2 = '\0'

ReadFile(sClient)

return true

}

// 传送文件

bool SendFile(SOCKET sClient)

{

if (NULL == g_pBuff)

{

ReadFile(sClient)

}

int nPacketBufferSize = MAX_PACKET_SIZE - 2 * sizeof(int)// 每个数据包存放文件的buffer大小

// 如果文件的长度大于每个数据包所能传送的buffer长度那么就分块传送

for (int i = 0i <g_lLengthi += nPacketBufferSize)

{

CCSDef::TMSG_FILE tMsgFile

tMsgFile.tFile.nStart = i

if (i + nPacketBufferSize + 1>g_lLength)

{

tMsgFile.tFile.nSize = g_lLength - i

}

else

{

tMsgFile.tFile.nSize = nPacketBufferSize

}

//printf("start = %d, size = %d\n", tMsgFile.tFile.nStart, tMsgFile.tFile.nSize)

memcpy(tMsgFile.tFile.szBuff, g_pBuff + tMsgFile.tFile.nStart, tMsgFile.tFile.nSize)

::send(sClient, (char*)(&tMsgFile), sizeof(CCSDef::TMSG_FILE), 0)

Sleep(0.5)

}

delete [] g_pBuff

g_pBuff = NULL

return true

}

/*client.h同server.h*/

/*client.cpp*/

#include "client.h"

long g_lLength = 0

char* g_pBuff = NULL

char g_szFileName[MAXFILEDIRLENGTH]

char g_szBuff[MAX_PACKET_SIZE + 1]

SOCKET g_sClient

// 初始化socket库

bool InitSocket()

// 关闭socket库

bool CloseSocket()

// 把用户输入的文件路径传送到server端

bool SendFileNameToServer()

// 与server端连接

bool ConectToServer()

// 打开文件失败

bool OpenFileError(CCSDef::TMSG_HEADER *pMsgHeader)

// 分配空间以便写入文件

bool AllocateMemoryForFile(CCSDef::TMSG_HEADER *pMsgHeader)

// 写入文件

bool WriteToFile(CCSDef::TMSG_HEADER *pMsgHeader)

// 处理server端传送过来的消息

bool ProcessMsg()

int main()

{

InitSocket()

ConectToServer()

CloseSocket()

return 0

}

// 初始化socket库

bool InitSocket()

{

// 初始化socket dll

WSADATA wsaData

WORD socketVersion = MAKEWORD(2, 2)

if (::WSAStartup(socketVersion, &wsaData) != 0)

{

printf("Init socket dll error\n")

exit(-1)

}

return true

}

// 关闭socket库

bool CloseSocket()

{

// 关闭套接字

::closesocket(g_sClient)

// 释放winsock库

::WSACleanup()

return true

}

// 与server端连接进行文件的传输

bool ConectToServer()

{

// 初始化socket套接字

if (SOCKET_ERROR == (g_sClient = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)))

{

printf("Init Socket Error!\n")

exit(-1)

}

sockaddr_in servAddr

servAddr.sin_family = AF_INET

servAddr.sin_port = htons(PORT)

servAddr.sin_addr.S_un.S_addr = ::inet_addr(SERVER_IP)

if (INVALID_SOCKET == (::connect(g_sClient, (sockaddr*)&servAddr, sizeof(sockaddr_in))))

{

printf("Connect to Server Error!\n")

exit(-1)

}

// 输入文件路径传输到server端

SendFileNameToServer()

// 接收server端传过来的信息,直到保存文件成功为止

while (true == ProcessMsg())

{

}

return true

}

// 把用户输入的文件路径传送到server端

bool SendFileNameToServer()

{

char szFileName[MAXFILEDIRLENGTH]

printf("Input the File Directory: ")

fgets(szFileName, MAXFILEDIRLENGTH, stdin)

// 把文件路径发到server端

CCSDef::TMSG_FILENAME tMsgRequestFileName

strcpy(tMsgRequestFileName.szFileName, szFileName)

if (SOCKET_ERROR == ::send(g_sClient, (char*)(&tMsgRequestFileName), sizeof(CCSDef::TMSG_FILENAME), 0))

{

printf("Send File Name Error!\n")

exit(-1)

}

return true

}

// 处理server端传送过来的消息

bool ProcessMsg()

{

CCSDef::TMSG_HEADER *pMsgHeader

int nRecv = ::recv(g_sClient, g_szBuff, MAX_PACKET_SIZE + 1, 0)

pMsgHeader = (CCSDef::TMSG_HEADER*)g_szBuff

switch (pMsgHeader->cMsgID)

{

case MSG_OPENFILE_ERROR: // 打开文件错误

{

OpenFileError(pMsgHeader)

}

break

case MSG_FILELENGTH: // 文件的长度

{

if (0 == g_lLength)

{

g_lLength = ((CCSDef::TMSG_FILELENGTH*)pMsgHeader)->lLength

printf("File Length: %d\n", g_lLength)

}

}

break

case MSG_FILENAME: // 文件名

{

return AllocateMemoryForFile(pMsgHeader)

}

break

case MSG_FILE: // 传送文件,写入文件成功之后退出这个函数

{

if (WriteToFile(pMsgHeader))

{

return false

}

}

break

}

return true

}

// 打开文件失败

bool OpenFileError(CCSDef::TMSG_HEADER *pMsgHeader)

{

if (NULL != g_pBuff)

return true

assert(NULL != pMsgHeader)

printf("Cannot find file!Please input again!\n")

// 重新输入文件名称

SendFileNameToServer()

return true

}

// 查找是否已经存在了要保存的文件,同时分配缓冲区保存文件

bool AllocateMemoryForFile(CCSDef::TMSG_HEADER *pMsgHeader)

{

assert(NULL != pMsgHeader)

if (NULL != g_pBuff)

{

return true

}

CCSDef::TMSG_FILENAME* pRequestFilenameMsg = (CCSDef::TMSG_FILENAME*)pMsgHeader

printf("File Name: %s\n", pRequestFilenameMsg->szFileName)

// 把文件的路径设置为C盘根目录下

strcpy(g_szFileName, "c:\\")

strcat(g_szFileName, pRequestFilenameMsg->szFileName)

// 查找相同文件名的文件是否已经存在,如果存在报错退出

FILE* pFile

if (NULL != (pFile = fopen(g_szFileName, "r")))

{

// 文件已经存在,要求重新输入一个文件

printf("The file already exist!\n")

CCSDef::TMSG_ERROR_MSG tMsgErrorMsg(MSG_FILEALREADYEXIT_ERROR)

::send(g_sClient, (char*)(&tMsgErrorMsg), sizeof(CCSDef::TMSG_ERROR_MSG), 0)

fclose(pFile)

return false

}

// 分配缓冲区开始接收文件,如果分配成功就给server端发送开始传送文件的要求

g_pBuff = new char[g_lLength + 1]

if (NULL != g_pBuff)

{

memset(g_pBuff, '\0', g_lLength + 1)

printf("Now ready to get the file %s!\n", pRequestFilenameMsg->szFileName)

CCSDef::TMSG_CLIENT_READY tMsgClientReady

if (SOCKET_ERROR == ::send(g_sClient, (char*)(&tMsgClientReady), sizeof(CCSDef::TMSG_CLIENT_READY), 0))

{

printf("Send Error!\n")

exit(-1)

}

}

else

{

printf("Alloc memory for file error!\n")

exit(-1)

}

return true

}

// 写入文件

bool WriteToFile(CCSDef::TMSG_HEADER *pMsgHeader)

{

assert(NULL != pMsgHeader)

CCSDef::TMSG_FILE* pMsgFile = (CCSDef::TMSG_FILE*)pMsgHeader

int nStart = pMsgFile->tFile.nStart

int nSize = pMsgFile->tFile.nSize

memcpy(g_pBuff + nStart, pMsgFile->tFile.szBuff, nSize)

if (0 == nStart)

{

printf("Saving file into buffer...\n")

}

memcpy(g_pBuff + nStart, pMsgFile->tFile.szBuff, nSize)

//printf("start = %d, size = %d\n", nStart, nSize)

// 如果已经保存到缓冲区完毕就写入文件

if (nStart + nSize >= g_lLength)

{

printf("Writing to disk....\n")

// 写入文件

FILE* pFile

pFile = fopen(g_szFileName, "w+b")

fwrite(g_pBuff, sizeof(char), g_lLength, pFile)

delete [] g_pBuff

g_pBuff = NULL

fclose(pFile)

// 保存文件成功传送消息给server退出server

CCSDef::TMSG_SENDFILESUCCESS tMsgSendFileSuccess

while (SOCKET_ERROR == ::send(g_sClient, (char*)(&tMsgSendFileSuccess), sizeof(CCSDef::TMSG_SENDFILESUCCESS), 0))

{

}

printf("Save the file %s success!\n", g_szFileName)

return true

}

else

{

return false

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存