提交 f8420398 编写于 作者: X xwhqsj

update socket

上级 90f592b6
# Default ignored files
/workspace.xml
\ No newline at end of file
callJvmThreadpool
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="EndlessLoop" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$/callJvmThreadpool">
<contentRoot DIR="$PROJECT_DIR$" />
</component>
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/CallJvm.iml" filepath="$PROJECT_DIR$/.idea/CallJvm.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -13,15 +13,15 @@
int main()
{
std::string plainsql, dbname;
int sock = 0, valread;
std::string plainsql, dbname, sendstream;
int sock = 0;
struct sockaddr_in serv_addr;
plainsql = "select * from test;";
char *psql = (char*)plainsql.data();
plainsql = "select * from test";
dbname = "student";
char *dbn = (char*)dbname.data();
char split = '$';
sendstream = plainsql + "$" + dbname;
char *sstr = (char*)sendstream.data();
printf("sendstream is %s\n", sstr);
char buffer[1024] = {0};
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
......@@ -46,12 +46,21 @@ int main()
return -1;
}
send(sock, psql, strlen(psql), 0);
printf("Hello message: %s sent\n", psql);
send(sock, reinterpret_cast<const void *>(split), 1, 0);
send(sock, dbn, strlen(dbn), 0);
printf("Talk message: %s send\n", dbn);
send(sock, sstr, strlen(sstr), 0);
printf("Hello message: %s sent\n", sstr);
valread = read(sock, buffer, 1024);
read(sock, buffer, 1024);
printf("%s\n", buffer);
}
\ No newline at end of file
}
int i;
for(i = 0; i < strlen(buffer); i++)
{
if(buffer[i] == '$')
{
strncpy(psql, buffer, i);
strncpy(dbn, buffer + i + 1, strlen(buffer) - i - 1);
}
}
printf("%s\n", psql);
printf("%s\n", dbn);
//
// Created by wanhui on 11/2/19.
//
#include <unistd.h>
#include <cstdio>
#include <sys/socket.h>
......@@ -9,69 +5,82 @@
#include <netinet/in.h>
#include <cstring>
#include <iostream>
#include <vector>
#define PORT 8080
int main() {
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *psql = nullptr;
char *dbn = nullptr;
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt))) {
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *) &address,
sizeof(address)) < 0) {
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *) &address,
(socklen_t *) &addrlen)) < 0) {
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
int i;
for(i = 0; i < strlen(buffer); i++)
send(new_socket , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
char* psql;
char* dbn;
char delims[] = "$";
char *res = nullptr;
std::vector<char*> resvec;
printf("%zu\n", strlen(buffer));
res = strtok(buffer, delims);
while (res != nullptr)
{
if(buffer[i] == '$')
{
strncpy(psql, buffer, i);
strncpy(dbn, buffer + i + 1, strlen(buffer) - i - 1);
}
resvec.push_back(res);
res = strtok(nullptr, delims);
}
psql = resvec[0];
dbn = resvec[1];
printf("%s\n", psql);
printf("%s\n", dbn);
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
return 0;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册