📄 client.cpp
字号:
// Client.cpp : Defines the entry point for the console application.
//
// 1. open the *.c in the Visual C++, then "rebuild all".
// 2. click "yes" to create a project workspace.
// 3. You need to -add the library 'ws2_32.lib' to your project
// (Project -> Properties -> Linker -> Input -> Additional Dependencies)
// 4. recompile the source.
#include "stdafx.h"
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#pragma comment (lib,"WS2_32.lib")
#define DEFAULT_PORT 5019
char szBuff[200];
SOCKET connect_sock;
int rece();
int send();
int msg_len;
time_t now;
FILE *stream;
int main(int argc, char **argv){
int ctr;
//int addr_len;
struct sockaddr_in server_addr;
struct hostent *hp;
WSADATA wsaData;
char *server_name = "localhost";
unsigned short port = DEFAULT_PORT;
unsigned int addr;
if (argc != 3){
printf("echoscln [server name] [port number]\n");
return -1;
}
else{
server_name = argv[1];
port = atoi(argv[2]);
}
if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR){
// stderr: standard error are printed to the screen.
fprintf(stderr, "WSAStartup failed with error %d\n", WSAGetLastError());
//WSACleanup function terminates use of the Windows Sockets DLL.
WSACleanup();
return -1;
}
if (isalpha(server_name[0]))
hp = gethostbyname(server_name);
else{
addr = inet_addr(server_name);
hp = gethostbyaddr((char*)&addr, 4, AF_INET);
}
if (hp==NULL)
{
fprintf(stderr, "Cannot resolve address: %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
//copy the resolved information into the sockaddr_in structure
memset(&server_addr, 0, sizeof(server_addr));
memcpy(&(server_addr.sin_addr), hp->h_addr, hp->h_length);
server_addr.sin_family = hp->h_addrtype;
server_addr.sin_port = htons(port);
connect_sock = socket(AF_INET,SOCK_STREAM, 0); //TCp socket
if (connect_sock == INVALID_SOCKET){
fprintf(stderr, "socket() failed with error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
printf("Client connecting to:GroupDns ==>%s\n", hp->h_name);
if (connect(connect_sock, (struct sockaddr *)&server_addr, sizeof(server_addr))
== SOCKET_ERROR){
fprintf(stderr, "connect() failed with error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
fopen_s(&stream,"log.txt","a+");
while(true)
{
printf("press '1' to receive message '2' to input words: (press 'ctrl + c'to quit)\n");
scanf("%d",&ctr);
switch(ctr)
{
case 2:
send();
break;
case 1:
rece();
break;
}
}
closesocket(connect_sock);
WSACleanup();
}
int send()
{
memset(szBuff, 0,sizeof(szBuff));
fflush(stdin);//clear the input stream
printf("input your word:\n");
gets(szBuff);
msg_len = send(connect_sock, szBuff, sizeof(szBuff), 0);
if (msg_len == SOCKET_ERROR){
fprintf(stderr, "send() failed with error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
if (msg_len == 0){
printf("server closed connection\n");
closesocket(connect_sock);
WSACleanup();
return -1;
}
return 0;
}
DWORD WINAPI recThread(LPVOID pParam)
{
memset(szBuff, 0,sizeof(szBuff));
msg_len = recv(connect_sock, szBuff, sizeof(szBuff), 0);
return 1;
}
int rece()
{
DWORD dwThreadId;
HANDLE hThread = CreateThread(NULL, 0, recThread, NULL, 0, &dwThreadId);
while(sizeof(szBuff)==0);
time(&now);
fprintf(stream,"\n\nThe Time : %s\n",ctime(&now));
//fprintf(stream,"Sender : %s\n",w);
fprintf(stream,"Receive : %s\n",szBuff);
if (msg_len == SOCKET_ERROR){
fprintf(stderr, "send() failed with error %d\n", WSAGetLastError());
closesocket(connect_sock);
WSACleanup();
return -1;
}
if (msg_len <= 0){
printf("retry\n");
//closesocket(connect_sock);
//WSACleanup();
return 0;
}
if (msg_len == NULL){
printf("Echo nothing");
}
else
{
time(&now);
printf(" %s | %s |.\n", ctime(&now),szBuff);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -