Network programming-Simple TCP client Server programming

Source: Internet
Author: User
Tags first string

Simple TCP Program Client process: Create socket (socket with IP address: port number) socket (), Request connection connect (), Exchange data Send ()/recv (), close connection closesocket ()

Simple TCP Program Server process: Create socket socket (), bind corresponding port bind (), listen port listen (), accept connection accept (), Exchange Data recv ()/send (), close connection Closesocket ()

First Network program

Code implementation:

TcpServer.cpp

TcpClient.cpp

On the basis of this, a simple TCP-based user registration program is implemented to achieve the following functions :

The client sends the user registration information (user name, password and other information) to the server side; the server side first checks whether the user registration data conforms to the format. Returns an "illegal access" prompt if it does not conform, or if the user's name is present in the checked users file, and if it does not exist, the user registration information is stored in the user file, and the "user registration succeeded" prompt returns "the user name has been registered" if the user name is the same.

The main implementation function:

Put user-typed data into the user structure
User input_info (const char* message);

Determine whether all data typed by the user conforms to the format
int Is_valid_format (const user user);

Determine whether the user name, password, mobile phone number is in accordance with the format
int is_valid_format_of_name (const char* Name);
int Is_valid_format_of_password (const char* pass);
int Is_valid_format_of_phonenumber (const char* phone);

Main validation functions
void Main_process_info (const User user,const SOCKET snewconnection);

Determine if the user name has been registered (that is, the user name does not exist in the user file)
int is_name_exist (const char* Name);

To write new user information to a file
int write_info_to_txt (const user user);

/**** the specific code of each function ***/


User input_info (const char* message)
{
User user;
Char username[20] = {0},
PASSWORD[20] = {0}, phonenumber[20] = {0};
user = {' n ', ' + ', ' + '};
User.username = {0};

SSCANF (Message, "%s\t%s\t%s", User.username,
User.password, User.phonenumber);

/*printf ("%s\t%s\t%s", User.username,
User.password, User.phonenumber); * *
/*SSCANF (Message, "%s\t%s\t%s", UserName,
password, phonenumber);
printf ("%s\t%s\t%s", UserName,
password, phonenumber); * *
return user;
}

int Is_valid_format (const user user)
{
int flag = 0;
if (Is_valid_format_of_name (user.username) &&
Is_valid_format_of_password (User.password) &&
Is_valid_format_of_phonenumber (User.phonenumber))
{
return 1;
}
Else
{
return 0;
}
}

int is_valid_format_of_name (const char* Name)
{
if (strlen (name) >= 6 && strlen (name) <= 12)
return 1;
Else
return 0;

}
int Is_valid_format_of_password (const char* Pass)
{
if (strlen (pass) >= 8 && strlen (pass) <= 24)
return 1;
Else
return 0;
}
int Is_valid_format_of_phonenumber (const char* phone)
{
if (strlen (phone) ==11)//First judge the length is not 11 bit, then judge is not all the numbers
{
for (int i = 0; i < one; i++)
{
if (Phone[i] >= ' 0 ' &&phone[i] <= ' 9 ')
{
Continue
}
Else
{
Break
return 0;
}

}
return 1;
}
Else
{
return 0;
}
}

int is_name_exist (const char* Name)
{
Open the user file, compare the user name and the user name in the file,
See if the same username already exists.
char* file_name = "User.txt";
if (Access (file_name, 0))
{
printf ("D://user.txt doesn ' t exist!");
printf ("then create!");
char* file_name = "User.txt";
FILE *FP = fopen (file_name, "at+");
}
Put all the records in the file.
Assume that each record hosts all of the information for each user
The first string is the name of the user
Char buf[max_size];/* buffer */
File* Fp=null;
if ((Fp=fopen ("User.txt", "R")) ==null)//the file and the. cpp file are placed in the same parent directory
{
printf ("Failed to read!");
Exit (1);
}
while (Fgets (BUF,MAX_SIZE,FP)!=null)
{
Char* temp_name;//will appear to stop working.
Char temp_name[256] = {'} '};
Char temp_pass[512] = {'} '};
Char temp_phone[256] = {'} '};
SSCANF (buf, "%s\t%s\t%s", Temp_name,temp_pass,temp_phone);
if (strcmp (temp_name,name) ==0)//The user name is already registered, the user name exists in the file
{
return 1;
}
Else
{
Continue
}
}
Return 0;//The user name is not registered and the user name does not exist in the user file

}

Void Main_process_info (const User user,const SOCKET snewconnection)
{
Char msg[256] = {'} '};
if (Is_valid_format)//If the user registration data conforms to the format
{
if (! Is_name_exist (user.username))
{
//writes the user's registration information to the user file
if (write_info_to_txt (user)
{
strcpy (msg, " User Registration Successful! \ n ");
Send (Snewconnection,msg,sizeof (msg), 0);
printf ("User registration succeeded!") \ n ");
}
Else
{
strcpy (msg, "User registration failed!") (Failed to write the new user information to the user file successfully) \ n ");
Send (Snewconnection, MSG, sizeof (msg), 0);
printf ("User registration failed!") (Failed to write the new user information to the user file successfully) \ n ");
}

}
Else
{
strcpy (msg, "The user name has already been registered!) \ n ");
Send (Snewconnection, MSG, sizeof (msg), 0);
printf ("The user name is already registered! \ n ");
}
}
Else
{
strcpy (msg, "illegal access! \ n ");
Send (Snewconnection, MSG, sizeof (msg), 0);
printf ("Illegal access! \ n ");
}
}

int write_info_to_txt (const user user)
{
file* FP;
char* filename = "User.txt";
if ((Fp=fopen (filename, "a")) ==null)
{
printf ("\ n cannot open the user file! \ n ");
return 0;
}
fprintf (FP, "%s\t%s\t%s\n", User.username,user.password,user.phonenumber);
printf ("%s\t%s\t%s\n", User.username, User.password, User.phonenumber);
Fclose (FP);
return 1;
}

The screenshot of the running result is as follows:

Client:

Service side:

User Information file:

Network programming-Simple TCP client Server programming

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.