C++ 怎么使用SOCKET连接MySQL服务器?

C++ 怎么使用SOCKET连接MySQL服务器?,第1张

首先需要把mysql中的头文件和库文件放入到c++项目工程中

1.打开安装MySQL的目录,在windows系统中如果是默认路径,应该和我的是一样的:C:\Program Files\MySQL\MySQL Server 8.0

只是做这些动作的话不需要多线程

找了下以前写的 改成了你说的10次发送

client.c:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<errno.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

int main(int argc,char **argv)

{

char wbuf[] = "hello server"

char rbuf[128]

int i

int sock

struct sockaddr_in server = {0}

struct timeval timeo

timeo.tv_sec = 0

timeo.tv_usec = 1000 * 1000 //

socklen_t len = sizeof(timeo)

if( argc != 2)

{

printf("usage: ./client <ipaddress>\n")

return -1

}

sock = socket(AF_INET, SOCK_STREAM, 0)

if(sock <0)

{

perror("Create TCP Socket")

return -1

}

setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeo, len)

server.sin_family = AF_INET

server.sin_port = htons(30000)

inet_pton(AF_INET, argv[1], &(server.sin_addr))

int res = connect(sock, (struct sockaddr*)&server, sizeof(server))

if (res <0)

{

if(res == EINPROGRESS)

perror("connecting stream socket time out:")

else

perror("connecting stream socket error:")

close(sock)

return -1

}

else

{

printf("Connect Server@%s\n",argv[1])

for(i=0i<10i++)

{

int wsize = send(sock,wbuf,sizeof(wbuf),0)

if(wsize<=0)

{

perror("write error:")

close(sock)

return -1

}

printf("1111111i=%d\n",i)

while(1)

{

int rsize=recv(sock,rbuf,sizeof(rbuf),0)

if(rsize>0)

{

rbuf[rsize]='\0'

printf("recv msg from server: %s\n",rbuf)

break

}

if(rsize<0)

{

close(sock)

perror("read error:")

return -1

}

}

}

close(sock)

return 0

}

}

server.c:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<errno.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

int main(int argc, char** argv)

{

intlistenfd, connfd

struct sockaddr_in servaddr = {0}

charrbuf[128]

charwbuf[] = "hello client"

int n

if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )

{

perror("Create TCP Socket")

return -1

}

servaddr.sin_family = AF_INET

servaddr.sin_addr.s_addr = htonl(INADDR_ANY)

servaddr.sin_port = htons(30000)

if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)

{

perror("bind socket error:")

return -1

}

if( listen(listenfd, 10) == -1)

{

perror("listen socket error:")

return -1

}

printf("======waiting for client's request======\n")

while(1)

{

if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1)

{

perror("accept socket error:")

continue

}

while(1)

{

n = recv(connfd, rbuf, sizeof(rbuf), 0)

if(n>0)

{

rbuf[n] = '\0'

printf("recvmsg from client: %s\n", rbuf)

n = send(connfd, wbuf, sizeof(wbuf),0)

if(n<=0)

{

perror("sned error:")

close(connfd)

break

}

}

else if(n <0)

{

perror("recv error:")

close(connfd)

break

}

}

close(connfd)

}

close(listenfd)

return 0

}

运行的时候 client

./client 你的serverip

端口我用的30000 写死在程序里了

Receive的返回值是接收的字节数,当是0时表示传送结束,否则循环接收

缓冲区不用太大,8k或64k看情况

Byte[] RecvBytes = new Byte[1024*8]

int iBytes = 1

string strAllHtml = ""

while (iBytes >0)

{

iBytes = conn.Receive(RecvBytes, RecvBytes.Length, 0)

strAllHtml += Encoding.UTF8.GetString(RecvBytes, 0, iBytes)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9590263.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-29
下一篇 2023-04-29

发表评论

登录后才能评论

评论列表(0条)

保存