我想创建一个客户端并将其连接到服务器。我知道客户端进程在服务器之前开始运行,因此我在无限循环中进行连接,直到它被接受为止。然后我想向客户端输入消息并将它们发送到服务器,服务器将在屏幕上打印它们。我从代码中删除了所有错误检测以缩短它,我知道它们必须在那里。我试图这样做,但服务器什么都没收到,客户端也无法做第二次连接。
服务器
int main(int argc, char *argv[])
{
int sockfd, newsockfd;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(5000);
bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
listen(sockfd, 5);
clilen = sizeof(cli_addr);
while (newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr,&clilen) >= 0)
{
read(newsockfd, buffer, 255);
close(newsockfd);
}
close(sockfd);
return 0;
}
客户端
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in serv_addr;
char buffer[256];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(5000);
while (1) {
while ((connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)) {
printf("trying to connect\n");
sleep(1);
}
printf("connected\nplease, enter a message\n");
scanf("%s", buffer);
write(sockfd, buffer, strlen(buffer));
}
close(sockfd);
return 0;
}
答案 0 :(得分:2)
我不知道您是如何编译代码的,但如果您设置了警告标记(gcc中为if (newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr,&clilen) >= 0) {
),您就会收到此警告
if ((newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr,&clilen)) >= 0) {
代理它,我改变了,这个..
int sockfd, newsockfd;
socklen_t clilen;
char buffer[256];
int nbytes = 0;
struct sockaddr_in serv_addr, cli_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
//serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(5007);
bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (listen(sockfd, 5) <0) { }
clilen = sizeof(cli_addr);
if ((newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr,&clilen)) >= 0) {
while ((nbytes = read(newsockfd, buffer, sizeof(buffer)))<0) {
sleep(1);
}
printf("client sent: %s", buffer);
}
close(newsockfd);
close(sockfd);
return 0;
要......(注意额外的括号)
<!DOCTYPE html>
<html>
<head>
<title> Motors </title>
<script src="jquery-3.1.1.js"></script>
<link href="style.css" rel = "stylesheet" type="text/CSS" />
</head>
<body>
<header>
<h1 id="cmpName">
MOTORS
</h1>
<nav>
<ul id = "navigation">
<li> <a href="#"> HOME </a> </li>
<li> <a href="#"> CATALOGUE </a> </li>
<li> <a href="#"> ACHIVEMENTS </a> </li>
<li> <a href="#"> ABOUT US </a> </li>
<li> <a href="#"> CONTACT </a> </li>
</ul>
</nav>
<div id = "slideDiv">
<img class="slideImg" src="http://eskipaper.com/images/toyota-wallpaper-2.jpg" />
<img class="slideImg" src="http://www.hdwallpapers.in/walls/2013_lamborghini_aventador_lp700_4-wide.jpg"/>
<img class="slideImg" src="https://images7.alphacoders.com/318/318873.jpg"/>
<img class="slideImg" src="https://wallpaperscraft.com/image/ferrari_red_cars_sport_95424_3840x2400.jpg"/>
</div>
</header>
<section id="mainContent">
<section class="oneNews">
<h3 class = "newsHeader"> New Headquarters </h3>
<img src="http://acdn.architizer.com/thumbnails-PRODUCTION/ee/87/ee87914abbec5ba91890ea35110e7a0b.jpg" class = "newsImg"/>
<p class="newsDscr"> The headquarters for Spanish energy utility provider Endesa fuses innovative workplace design with a progressive environmental program. </p>
</section>
<section class="oneNews">
<h3 class = "newsHeader"> Shchumacher Wins </h3>
<img src="https://formula1.files.wordpress.com/2011/03/season2011_race1_wallpapers_01_1280x1024.jpg" class = "newsImg"/>
<p class="newsDscr"> In winning the Drivers' Championship he equalled the record set by Juan Manuel Fangio of five World Championships. Ferrari won 15 out of 17 races, and Schumacher won the title with six races remaining in the season. </p>
</section>
</section>
</body>
</html>
我没有再收到其他警告,服务器抓住了客户的消息。
这是我在调试时运行的服务器(这基本上是你的代码稍有改动)
@import url('https://fonts.googleapis.com/css?family=Inconsolata');
html,body{
margin: 0;
padding: 0;
background-color: #708ebf;
}
a {
text-decoration: none;
color: black;
}
header {
width: 900px;
background-color: #0543a8;
margin-left: auto;
margin-right: auto;
padding-bottom: 0;
padding-top: 0;
}
#cmpName {
text-align: center;
font-family: 'Inconsolata', monospace;
font-size: 60px;
margin: 0;
}
nav {
margin: 0;
}
#navigation {
background-color: #0d52c1;
text-align: center;
padding: 0;
margin: 0;
margin-bottom: 20px;
margin-top: 20px;
}
#navigation li {
list-style-type: none;
font-family: 'Inconsolata', monospace;
display: inline;
font-size: 30px;
margin-left: 19px;
margin-right: 19px;
color: black;
}
#slideDiv{
width: 900px;
height: 450px;
margin-left: auto;
margin-right: auto;
position: relative;
right: 5px;
margin-bottom: 0;
padding-bottom: 0;
}
.slideImg {
width: 900px;
height: 450px;
display: inline;
position: absolute;
margin: 5px;
margin-top: 0;
margin-bottom: 0;
padding-bottom: 0;
}
#mainContent {
width: 900px;
margin-left: auto;
margin-right: auto;
border: solid;
padding: 0;
}
.oneNews {
display: inline-block;
border: solid;
margin: 10px;
width: 270px;
height: 500px;
padding: 0;
}
.newsImg {
width: 270px;
height: 150px;
padding: 0;
margin: 0;
}
.newsDscr {
width: 270px;
text-align: center;
height: 120px;
margin: 0;
padding: 0;
}
.newsHeader {
width: 270px;
text-align: center;
padding: 0;
margin: 0;
}
答案 1 :(得分:0)
另一个问题是我们每次在客户端连接时都必须创建套接字,例如:
while (1) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
while ((connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)) {
printf("trying to connect\n");
sleep(1);
}
printf("connected\nplease, enter a message\n");
scanf("%s", buffer);
write(sockfd, buffer, strlen(buffer));
}