简体   繁体   English

对TCP套接字服务器的jQuery GET请求

[英]JQuery GET request to TCP socket server

I ve written a really simple TCP server in C that uses sockets. 我用C编写了一个使用套接字的非常简单的TCP服务器。 I'd like to establish a back and forth from the server to a web interface using JQuery get and post operations. 我想使用JQuery的get和post操作在服务器和Web界面之间建立往返关系。

At this stage, the only functionality i have implemented is the GET operation, and the server listens and returns a string upon connection. 在此阶段,我实现的唯一功能是GET操作,服务器在连接时侦听并返回字符串。 This works, however, not with JQuery (or JS). 但是,这不适用于JQuery(或JS)。 The odd thing is when i directly input the servers IP and port into the browsers address bar, the string is displayed? 奇怪的是,当我直接在浏览器地址栏中输入服务器的IP和端口时,会显示字符串吗?

I am using box standard JQuery on a local host. 我在本地主机上使用标准的JQuery。

Any suggestions? 有什么建议么?

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.get("127.0.0.1:800",function(data){
            alert("Data: " + data );
    });
  });
 });
</script>
</head>
<body>

<button>GET</button>

</body>
</html>

Server 服务器

#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
  int sockd, sockd2;
  int addrlen;
  struct sockaddr_in my_name, peer_name;
  int status;

  /* create a socket */
  sockd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockd == -1)
  {
    perror("Socket creation error");
    exit(1);
  }

  if (argc < 2)
  {
    fprintf(stderr, "Usage: %s port_number\n", argv[0]);
    exit(1);
  }

  /* server address  */
  my_name.sin_family = AF_INET;
  my_name.sin_addr.s_addr = INADDR_ANY;
  my_name.sin_port = htons(atoi(argv[1]));

  status = bind(sockd, (struct sockaddr*)&my_name, sizeof(my_name));
  if (status == -1)
  {
    perror("Binding error");
    exit(1);
  }

  status = listen(sockd, 5);
  if (status == -1)
  {
    perror("Listening error");
    exit(1);
  }

  for(;;)
  {
    /* wait for a connection */
    addrlen = sizeof(peer_name);
    sockd2 = accept(sockd, (struct sockaddr*)&peer_name, &addrlen);
    if (sockd2 == -1)
    {
      perror("Wrong connection");
      exit(1);
    }
    write(sockd2, "Hello!\n", 7);
    close(sockd2);
  }
  return 0;
}

JQuery expects your server to be an HTTP server, but you are not speaking HTTP. JQuery希望您的服务器是HTTP服务器,但您不是在说HTTP。 Your browser shouldn't be able to display your message either. 您的浏览器将无法显示您的消息。

In HTTP the client sends a request, then the server sends a response. 在HTTP中,客户端发送请求,然后服务器发送响应。 The connection can then be closed immediately (simplest) or reused for future requests (because it's faster than making a new connection). 然后可以立即关闭该连接(最简单)或将其重新用于以后的请求(因为它比建立新连接要快)。

A bare-minimum HTTP request (what the client sends) looks like this: 最低限度的HTTP请求(客户端发送的请求)如下所示:

GET / HTTP/1.1
Host: 127.0.0.1

and the response (what the server sends) looks like this: 响应(服务器发送的内容)如下所示:

HTTP/1.1 200 OK
Connection: close
Content-Length: 6
Content-Type: text/plain

Hello!

The server would close the connection after sending the response. 发送响应后,服务器将关闭连接。 It must close the connection "gracefully" with shutdown - not just with close as this might prevent the client from seeing the entire response. 它必须关闭与连接“正常” shutdown - 不仅仅是 close ,因为这可能会阻止客户端看到整个响应。

Note that there is a \\r\\n at the end of each line, not just \\n . 请注意,每行的末尾都有一个\\r\\n ,而不仅仅是\\n

You should at least read RFC 2616 which is the HTTP/1.1 specification, if you want to write a correct web server. 如果要编写正确的Web服务器,则至少应阅读HTTP / 1.1规范RFC 2616 (Of course, if you're after a quick hack, you could ignore whatever the client sends, and always send a fixed response like the one above.) (当然,如果您要进行快速黑客攻击,则可以忽略客户端发送的任何内容,并始终发送固定的响应,如上面的响应。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery .get()不向服务器发送请求 - jQuery .get() not sending request to server 服务器收到带有 jQuery.ajax PATCH 请求的 GET 请求 - Server gets a GET request with jQuery .ajax PATCH request 我的jquery收到对服务器的请求超时后是否中止 - Does my jquery get request to server get aborted after timeout 获取定制的Jquery数据表GET请求参数,这些参数将发送到服务器 - Get to customize Jquery datatables GET request parameters that will be sent to the server jQuery发布并在本地Intranet和实时服务器上获取不同的请求 - jquery post and get request different on local intranet and live server 无法请求使用JQuery将GET API部署到本地Tomcat服务器? - Could not request GET API deployed to local Tomcat server using JQuery? 使用Phantomjs / Jquery发送Crossdomain GET请求服务器端 - Send a Crossdomain GET request Server Side with Phantomjs/Jquery JQuery Ajax通过分组获取获取解析API服务器的请求 - JQuery Ajax Get Request to Parse API server with grouping jQuery Img Attr请求获取服务器发送的标头 - JQuery Img Attr request get headers sent by the server 如何强制jQuery向服务器发出GET请求以刷新缓存的资源 - How to force jQuery to issue GET request to server to refresh cached resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM