The __c++ of C + + pen Test

Source: Internet
Author: User

http://m.blog.csdn.net/blog/zchen22/7906734


Huawei C language Face test

6. C Language file reading and writing
#include "stdio.h"
Main ()
{
FILE *FP;
Char ch,filename[10];
scanf ("%s", filename);
if ((Fp=fopen (filename, "w") ==null)
{
printf ("Cann ' t Open filen");
Exit (0);
}
Ch=getchar ();
while (ch!= ' # ')
{
FPUTC (CH,FP);
Putchar (CH);
Ch=getchar ();
}
Fclose (FP);
}

7. WinSocket Programming//This is good.
Server code
#include <Winsock2.h>
#include <stdio.h>
void Main ()
{
WORD wversionrequested; Version number
Wsadata Wsadata; Data
int err;
wversionrequested = Makeword (1,1);
Err = WSAStartup (wversionrequested,&wsadata);
if (Err!= 0)
{
Return
}
if (Lobyte (wsadata.wversion)!= 1| |
Hibyte (wsadata.wversion)!= 1)
{
WSACleanup ();
Return
}
SOCKET Socksrv=socket (af_inet,sock_stream,0); Set up sockets
Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_addr=htonl (Inaddr_any);
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port=htons (6000);
Bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr)); Binding ports
Listen (socksrv,5); Convert socket sockets to listening sockets
Sockaddr_in addrclient;
int len=sizeof (SOCKADDR);
while (1)//infinite loop
{
SOCKET sockconn=accept (Socksrv, (sockaddr*) &addrclient,&len);
Char sendbuf[100];
Sprint (SendBuf, "Welcome%s to http://www.sunxin.org"),
Inet_ntoa (ADDRCLIENT.SIN_ADDR));
Send (Sockconn,sendbuf,strlen (SENDBUF) +1,0);
Char recvbuf[100];
Recv (SOCKCONN,RECVBUF);
printf ("%sn", recvbuf);
Closesocket (Sockconn);
WSACleanup ();
}
}
Note: This is the server side; file->new->win32 Console application, project name: Tcpsrv; then, file->new->c++
Source file, filename: tcpsrv; In the project's setting Link's object/library modules entry to join Ws2_32.lib
#include <Winsock2.h>
#include <stdio.h>
void Main ()
{
WORD wversionrequested;
Wsadata Wsadata;
int err;
wversionrequested = Makeword (1,1);
Err = WSAStartup (wversionrequested,&wsadata); Start Winsock Dll
if (Err!= 0)
{
Return
}
if (Lobyte (wsadata.wversion)!= 1| |
Hibyte (wsadata.wversion)!= 1)
{
WSACleanup ();
Return
}
SOCKET Sockclient=socket (af_inet,sock_stream,0);
Sockaddr_in addrsrv;
Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
Addrsrv.sin_family=af_inet;
Addrsrv.sin_port = htons (6000);
Connect (sockclient, (sockaddr*) &addrsrv,sizeof (sockaddr));
Char recvbuf[100];
Recv (sockclient,recvbuf,100,0);
printf ("%sn", recvbuf);
Send (Sockclient, "This are Zhangsan", strlen ("This is Zhangsan") +1,0);
Closesocket (sockclient);
WSACleanup ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.