要实现客户端与服务器端的通讯,需要两部分C++的程序:服务器端程序代码和客户端代码。
首先是服务器端程序代码:
#include <WinSock2.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "ws2_32.lib")
void main()
{
WSADATA wsaData
int port = 5099
char buf[] = "Server: hello, I am a server....."
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock")
return
}
//创建用于监听的套接字
SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0)
SOCKADDR_IN addrSrv
addrSrv.sin_family = AF_INET
addrSrv.sin_port = htons(port)//1024以上的端口号
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY)
int retVal = bind(sockSrv, (LPSOCKADDR)&addrSrv, sizeof(SOCKADDR_IN))
if(retVal == SOCKET_ERROR){
printf("Failed bind:%d\n", WSAGetLastError())
return
}
if(listen(sockSrv,10) ==SOCKET_ERROR){
printf("Listen failed:%d", WSAGetLastError())
return
}
SOCKADDR_IN addrClient
int len = sizeof(SOCKADDR)
while(1)
{
//等待客户请求到来
SOCKET sockConn = accept(sockSrv, (SOCKADDR *) &addrClient, &len)
if(sockConn == SOCKET_ERROR){
printf("Accept failed:%d", WSAGetLastError())
break
}
printf("Accept client IP:[%s]\n", inet_ntoa(addrClient.sin_addr))
//发送数据
int iSend = send(sockConn, buf, sizeof(buf) , 0)
if(iSend == SOCKET_ERROR){
printf("send failed")
break
}
char recvBuf[100]
memset(recvBuf, 0, sizeof(recvBuf))
// //接收数据
recv(sockConn, recvBuf, sizeof(recvBuf), 0)
printf("%s\n", recvBuf)
closesocket(sockConn)
}
closesocket(sockSrv)
WSACleanup()
system("pause")
}
接下来是客户端代码:
#include <WinSock2.h>#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
void main()
{
//加载套接字
WSADATA wsaData
char buff[1024]
memset(buff, 0, sizeof(buff))
if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock")
return
}
SOCKADDR_IN addrSrv
addrSrv.sin_family = AF_INET
addrSrv.sin_port = htons(5099)
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1")
//创建套接字
SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0)
if(SOCKET_ERROR == sockClient){
printf("Socket() error:%d", WSAGetLastError())
return
}
//向服务器发出连接请求
if(connect(sockClient, (struct sockaddr*)&addrSrv, sizeof(addrSrv)) == INVALID_SOCKET){
printf("Connect failed:%d", WSAGetLastError())
return
}else
{
//接收数据
recv(sockClient, buff, sizeof(buff), 0)
printf("%s\n", buff)
}
//发送数据
char buff = "hello, this is a Client...."
send(sockClient, buff, sizeof(buff), 0)
//关闭套接字
closesocket(sockClient)
WSACleanup()
}
1.最简单的方法:public static String reverse1(String str)
{
return new StringBuffer(str).reverse().toString()
}
2.最常用的方法:
public static String reverse3(String s)
{
char[] array = s.toCharArray()
String reverse = "" //注意这是空串,不是null
for (int i = array.length - 1 i >= 0 i--)
reverse += array[i]
return reverse
}
3.常用方法的变形:
public static String reverse2(String s)
{
int length = s.length()
String reverse = "" //注意这是空串,不是null
for (int i = 0 i < length i++)
reverse = s.charAt(i) + reverse//在字符串前面连接, 而非常见的后面
return reverse
}
4.C语言中常用的方法:
public static String reverse5(String orig)
{
char[] s = orig.toCharArray()
int n = s.length - 1
int halfLength = n / 2
for (int i = 0 i <= halfLength i++) {
char temp = s[i]
s[i] = s[n - i]
s[n - i] = temp
}
return new String(s) //知道 char数组和String相互转化
}
在服务器上要开发一个TCP服务端,功能是维护在线客户端列表、处理客户端(假设为客户1)连接请求,将客户1的请求推送到客户2,根据客户2 的响应决定是不是在客户1和2之间建立应用连接,一旦应用连接建立,服务端就持续交换转发客户1、2的数据,直到其中一方断开,这是所有数据都通过服务器中转的方式;还有一种方式服务端仅仅维护在线用户列表,客户端相互直接连接,数据不通过服务器中转,具体实现还有许多细节需要处理,希望能够帮到你。欢迎分享,转载请注明来源:夏雨云
评论列表(0条)