1、http底层是通过socket实现的吗
1。socket是网络数据传输层主要技术手段,要传递网络信息的大都是靠socket实现的。2.jsp在是这么传输数据的专:先转属换成servlet,再编译为.class字节码servlet底层是socket,jsp所转化的servlet是HttpServlet,它是对http协议的封装。你可以去web服务器比如tomcat下的work文件夹看看,里面都是jsp转换的servelt以及他们的字节码.class文件。总的来说呢,底层到高层这么来的:socket->HttpServlet->JSP所以你非要说jsp底层是有socket实现的也没错。
2、JAVA 使用Socket 访问HTTP服务器
你先看看http协议吧。
你连上去 你没有发请求,服务器怎么知道你要干什么?
可以像下面一样。
String host = InetAddress.getByName("www.javathinker.org").getHostAddress();3、如何让SOCKET服务和HTTP服务跟着服务器一起启动,求解
你可以把你来的Socket服务写在源一个Servele的Init方法里面,然后在web.xml中配置
<servlet>就可以一起启动了。
4、C++ 如何使用 Socket 类向 HTTP 服务器发送数据和接收响应
客户端发送请求给服务器,服务器处理完毕以后把Socket tempSocket = (你还可以检测收到的1W8数据是3W8里面的哪一部分. 这个把发送的数据和接收的
5、增样基于http协议使用Socket写一个web服务器和一个web浏览器
最简单的http请求是
GET / HTTP/1.0
其中GET是关键字
/表示请求的是跟目录主页
HTTP/1.0表示使用HTTP协议的1.0版本通讯
作为服务器端,这是会向服务器返回首页的全部html代码
至此,一个最简单的http通讯就完成了。但是,此时网页还不能正常显示,因为服务器仅仅返回了html代码,客户端还必须根据html代码中涉及的其他文件(例如css、js、图象、flash等等)的地址获得相应文件,才能使网页正常显示
下面是http server和client的最简单实现(java)
import java.net.*;
import java.io.*;
import java.util.*;
public class Client
{
public static void main(String[] args)
{
String arg0;
if(args.length!=1)
{
System.out.println("Server name not founud!");
return;
}
else
{
arg0=args[0];
if(arg0.toLowerCase().indexOf("http://")!=0)
arg0="http://"+arg0;
}
try
{
java.net.URL theURL=new java.net.URL(arg0);
java.net.URLConnection conn=theURL.openConnection();
if(conn instanceof java.net.HttpURLConnection)
{
java.net.HttpURLConnection hConn=(HttpURLConnection)conn;
hConn.setFollowRedirects(false);
hConn.connect();
int code=hConn.getResponseCode();
String msg=hConn.getResponseMessage();
if(code==HttpURLConnection.HTTP_OK)
{
System.out.println("Normal response returned:" + code + " " +msg);
java.util.Map headerMap = hConn.getHeaderFields();
java.util.Iterator iterEnt = headerMap.entrySet().iterator();
while(iterEnt.hasNext())
{
System.out.println(iterEnt.next());
}
System.out.println("Hit enter to continue");
System.in.read();
java.io.InputStream in=hConn.getInputStream();
java.io.BufferedInputStream bufIn = new BufferedInputStream(in);
for(;;)
{
int data = bufIn.read();
if(data == -1)
break;
else
System.out.print( (char)data);
}}
else
{
System.out.println("Abormal response returned:" + code + " " +msg);
}}
else
{
System.err.println("Invalid transport protocol: not HTTP!");
return;
}}
catch(java.net.MalformedURLException ex)
{
System.err.println("Unable to parse URL!");
return;
}
catch(java.io.IOException ex)
{
System.err.println("I/O Error: "+ex);
return;
}}}
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
protected String docroot;
protected int port;
protected ServerSocket ss;
class Handler extends Thread
{
protected Socket socket;
protected PrintWriter pw;
protected BufferedOutputStream bos;
protected BufferedReader br;
protected File docroot;
public Handler(Socket _socket, String _docroot)throws Exception
{
socket=_socket;
docroot= new File(_docroot).getCanonicalFile() ;
}
public void run()
{
try{
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
bos=new BufferedOutputStream(socket.getOutputStream());
pw=new PrintWriter(new OutputStreamWriter(bos));
String line= br.readLine();
socket.shutdownInput();
if(line==null)
{
socket.close();
return;
}
if(line.toUpperCase().startsWith("GET"))
{
StringTokenizer tokens=new StringTokenizer(line," ?");
tokens.nextToken();
String req = tokens.nextToken() ;
String name;
if(req.startsWith("/") || req.startsWith("\\"))
name=this.docroot + req;
else
name=this.docroot+File.separator+req;
File file=new File(name).getCanonicalFile();
if(!file.getAbsolutePath().startsWith(this.docroot.getAbsolutePath()))
{
pw.println("HTTP/1.0 403 Forbidden");
pw.println();
}
else if(!file.exists())
{
pw.println("HTTP/1.0 404 File Not Found");
pw.println();
}
else if(!file.canRead())
{
pw.println("HTTP/1.0 403 Forbidden");
pw.println();
}
else if(file.isDirectory())
{
sendDir(bos,pw,file,req);
}
else
{
sendFile(bos,pw,file.getAbsolutePath());
}
else
{
pw.println("HTTP/1.0 501 Not Implemented");
pw.println();
}
pw.flush();
bos.flush();
}
catch(Exception ex)
{
ex.printStackTrace();
}
try
{
socket.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
protected void sendFile(BufferedOutputStream bos, PrintWriter pw, String filename) throws Exception
{
try
{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
byte[] data = new byte[10*1024];
int read= bis.read(data);
pw.println("HTTP/1.0 200 Okay");
pw.println();
pw.flush();
bos.flush();
while(read != -1)
{
bos.write(data, 0, read);
read = bis.read(data);
}
bos.flush();
}
catch(Exception ex)
{
pw.flush();
bos.flush();
}
}
protected void sendDir(BufferedOutputStream bos, PrintWriter pw, File dir, String req)throws Exception
{
try
{
pw.println("HTTP/1.0 200 Okay");
pw.println();
pw.flush();
pw.print("<html><head><titleDirectory of ");
pw.print(req);
pw.print("</title></head><body><h1>Directory of ");
pw.print(req);
pw.println("</h1><table border=0>");
File[] contents = dir.listFiles();
for(int i=0; i<contents.length; i++)
{
pw.print("<tr>");
pw.print("<td><a href=\"");
pw.print(req);
pw.print(contents[i].getName());
if(contents[i].isDirectory())
pw.print("/");
pw.print("\">");
if(contents[i].isDirectory())
pw.print("Dir -> ");
pw.print(contents[i].getName());
pw.print("</a></td>");
pw.println("</tr>");
}
pw.println("</table></body></html>");
pw.flush();
}
catch(Exception ex)
{
pw.flush();
bos.flush();
}}}
protected void parseParams(String[] args)
{
switch(args.length)
{
case 1:
case 0:
System.err.println("Syntax: <jvm> "+ this.getClass().getName()+" docroot port");
System.exit(0);
default:
this.docroot= args[0];
this.port = Integer.parseInt(args[1]);
break;
}}
public Server(String[] args)throws Exception
{
System.out.println("Checking for paramaters");
parseParams(args);
System.out.print("Starting web server......");
this.ss = new ServerSocket(this.port);
System.out.println("OK");
for(;;)
{
Socket accept = ss.accept();
new Handler(accept, docroot).start();
}}
public static void main(String[] args)throws Exception
{
Server ser= new Server(args);
}}
6、如何利用socket进行HTTP访问
你要用socket,写入流的数据就要符合HTTP协议规范,直接用HTTP连接撒。
java.net.HttpURLConnection
7、用JSP写一个Socket连接的HTTP服务器
socket正常写啊,你在servlet里面直接调用就可以了啊~~~~
8、python 怎么实现http服务器
简而言之,它是在物理服务器上搭建的一个网络连接服务器(networking server),永久地等待客户端发送请求。当服务器收到请求之后,它会生成响应并将 其返回至客户端。客户端与服务器之间的通信,是以HTTP协议进行的。客户端可以是浏览器,也可以是任何支持HTTP协议的软件。
那么,网络服务器的简单实现形式会是怎样的呢?下面是我对此的理解。示例代码使用Python语言实现,不过即使你不懂Python语言,你应该也可以从代码和下面的 解释中理解相关的概念:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import socket
HOST, PORT = '', 8888
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print 'Serving HTTP on port %s ...' % POR
9、设计一个linux c语言,Http协议的服务器,用socket收发消息,简单点,如何实现。求高手解答
去看一下《Advanced Linux Programming》这本书吧,第11章讲的就是怎么用C语言实现一Http服务器。
这里有下载地址(英文的):
http://www.advancedlinuxprogramming.com/alp-folder
英文看起来不顺的话可以上网找找有没有中文版的这本书,应该叫Linux高级编程吧~~~