资源简介
Windows网络编程第二版源码(补充材料.)
代码片段和文件信息
// Module Name: tcpclient.cpp
//
// Description:
//
// This sample illustrates how to develop a simple TCP client application
// that can send a simple “hello“ message to a TCP server listening on port 5150.
// This sample is implemented as a console-style application and simply prints
// status messages a connection is made and when data is sent to the server.
//
// Compile:
//
// cl -o tcpclient tcpclient.cpp ws2_32.lib
//
// Command Line Options:
//
// tcpclient.exe
//
#include
#include
void main(int argc char **argv)
{
WSADATA wsaData;
SOCKET s;
SOCKADDR_IN ServerAddr;
int Port = 5150;
int Ret;
if (argc <= 1)
{
printf(“USAGE: tcpclient .\n“);
return;
}
// Initialize Winsock version 2.2
if ((Ret = WSAStartup(MAKEWORD(22) &wsaData)) != 0)
{
// NOTE: Since Winsock failed to load we cannot use WSAGetLastError
// to determine the error code as is normally done when a Winsock
// API fails. We have to report the return status of the function.
printf(“WSAStartup failed with error %d\n“ Ret);
return;
}
// Create a new socket to make a client connection.
if ((s = socket(AF_INET SOCK_STREAM IPPROTO_TCP))
== INVALID_SOCKET)
{
printf(“socket failed with error %d\n“ WSAGetLastError());
WSACleanup();
return;
}
// Setup a SOCKADDR_IN structure that will be used to connect
// to a listening server on port 5150. For demonstration
// purposes we required the user to supply an IP address
// on the command line and we filled this field in with the
// data from the user.
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = inet_addr(argv[1]);
// Make a connection to the server with socket s.
printf(“We are trying to connect to %s:%d...\n“
inet_ntoa(ServerAddr.sin_addr) htons(ServerAddr.sin_port));
if (connect(s (SOCKADDR *) &ServerAddr sizeof(ServerAddr))
== SOCKET_ERROR)
{
printf(“connect failed with error %d\n“ WSAGetLastError());
closesocket(s);
WSACleanup();
return;
}
printf(“Our connection succeeded.\n“);
// At this point you can start sending or receiving data on
// the socket s. We will just send a hello message to the server.
printf(“We will now try to send a hello message.\n“);
if ((Ret = send(s “Hello“ 5 0)) == SOCKET_ERROR)
{
printf(“send failed with error %d\n“ WSAGetLastError());
closesocket(s);
WSACleanup();
return;
}
printf(“We successfully sent %d byte(s).\n“ Ret);
// When you are finished sending and receiving data on socket s
// you should close the socket.
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 47 2002-01-18 20:00 Windows网络编程补充材料\AUTORUN.INF
文件 2935 2002-08-29 11:09 Windows网络编程补充材料\Readme.txt
文件 192512 2002-01-18 20:00 Windows网络编程补充材料\StartCD.exe
文件 4015 2002-08-30 09:32 Windows网络编程补充材料\StartCD.ini
文件 167358 2002-10-18 14:41 Windows网络编程补充材料\补充内容\chapter17.pdf
文件 104818 2002-10-18 16:04 Windows网络编程补充材料\补充内容\chapter18.pdf
文件 31785 2002-10-18 16:04 Windows网络编程补充材料\补充内容\chapter19.pdf
文件 82346 2002-10-18 16:04 Windows网络编程补充材料\补充内容\chapter20.pdf
文件 17563 2002-10-18 16:04 Windows网络编程补充材料\补充内容\chapter21.pdf
文件 60126 2002-10-18 16:04 Windows网络编程补充材料\补充内容\chapter22.pdf
文件 148776 2002-01-18 20:00 Windows网络编程补充材料\StartCD\back3.BMP
文件 93656 2002-01-18 20:00 Windows网络编程补充材料\StartCD\bann3.bmp
文件 155502 2002-08-29 12:04 Windows网络编程补充材料\StartCD\cover.BMP
..A..H. 159744 2002-01-18 20:00 Windows网络编程补充材料\Setup\MSPUNIN.EXE
文件 262144 2002-01-18 20:00 Windows网络编程补充材料\Setup\Setup.exe
..A..H. 456 2002-01-18 20:00 Windows网络编程补充材料\Setup\Setup.ini
..A..H. 3475 2002-01-18 20:00 Windows网络编程补充材料\Setup\SETUP.TXT
文件 685 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\readme.txt
文件 2429 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\ThreadServer\Threads.cpp
文件 2058 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\Server\Server.cpp
文件 682 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\Server\VB\server.vbp
文件 3552 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\Server\VB\servervb.bas
文件 841 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\Server\VB\svrfrm.frm
文件 5020 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\OverlappedServer\Overlap.cpp
文件 1818 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\Client\Client.cpp
文件 691 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\Client\VB\client.vbp
文件 1340 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\Client\VB\clientvb.bas
文件 3532 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter20\Client\VB\pipec.frm
文件 515 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter19\Server2\Makefile
文件 3360 2002-01-18 20:00 Windows网络编程补充材料\Samples\chapter19\Server2\server.cpp
............此处省略384个文件信息
- 上一篇:SSH框架项目时序图
- 下一篇:谷歌图书器可以用于谷歌图书的
评论
共有 条评论