服务端程序:
import java.io.*;
import java.net.*;public class MyServer {
public static void main(String args[]) throws Exception{
ServerSocket serverSocket = new ServerSocket(8088);
System.out.println("server is ok.");
while(true){
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// DataInputStream in = new DataInputStream(socket.getInputStream());
String line = in.readLine();
//String line=in.toString();
System.out.println(line);
while(line!=null)
{
System.out.println("Client: " + line);
line = in.readLine();
// line=in.toString();
}
System.out.println("current user close the session.");
in.close();
socket.close();
}
}
}
客户端程序
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException; class HttpC{
private static String defaultURL = "http://127.0.0.1:8088";
private URL url;
HttpURLConnection hcon = null;
DataInputStream dis = null;
DataOutputStream dos = null;
String TAG = "HttpConnect";
public HttpC() throws MalformedURLException
{ url = new URL(defaultURL); if(url != null)
{
try
{
hcon = ( HttpURLConnection )url.openConnection(); //设置网络超时时间
hcon.setConnectTimeout(10000);
// 设置请求方法为POST
hcon.setRequestMethod("POST");
//设置输入输出流
hcon.setDoInput(true);
hcon.setDoOutput(true);
//Post请求不能使用缓存
hcon.setUseCaches(false);
hcon.setInstanceFollowRedirects(true);
//配置本次连接的Content-Type,配置为appplication/x-www-form-urlencoded
// hcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
hcon.connect();
}
catch( Exception e )
{
hcon = null;
e.printStackTrace();
// GenUtil.WriteLog("HttpConnect", e.toString().getBytes());
}
}
}
public boolean sendHttpPost(byte[] data)
{
if ( hcon == null )
{
return false;
}
try {
dos = new DataOutputStream(hcon.getOutputStream());
dos.write(data, 0, data.length);
dos.flush();
// GenUtil.WriteLog("SendData:", data);
}//
catch( Exception e )
{
e.printStackTrace();
// GenUtil.WriteLog("sendHttpPost", e.toString().getBytes());
return false;
}
return true;
} public byte[] recvHttpPost(int recvLen)
{
int Templen = 0;
int actual = 0;
byte[] recv = null;
int Tlen = 0;
if ( hcon == null )
{
return null;
}
try{
dis = new DataInputStream( hcon.getInputStream());
Tlen = hcon.getHeaderFieldInt("Content-Length",0);
recv = new byte[Tlen];
if(Tlen > 0 )
{
while((Templen != Tlen) && (actual != -1))
{
actual = dis.read(recv,Templen, Tlen - Templen);
Templen += actual;
}
}
}
catch( Exception e )
{
e.printStackTrace();
// GenUtil.WriteLog("recvHttpPost", e.toString().getBytes());
return null;
}
// GenUtil.WriteLog("Recv Tlen:", GenUtil.PaddingHexintToString(Tlen,4).getBytes());
// GenUtil.WriteLog("RecvData:", recv);
return recv;
}
public void stop()
{
try {
if ( hcon != null ) hcon.disconnect();
if( dis != null ) dis.close();
if( dos != null ) dos.close();
hcon = null;
dis = null;
dos = null;
}
catch ( IOException ioe )
{
ioe.printStackTrace();
// GenUtil.WriteLog("stop", ioe.toString().getBytes());
}//结束try/catch };//结束try/catch/finally
}
}
public class HttpConnect{
public static void main(String args[]) throws MalformedURLException
{
HttpC a=new HttpC();
String pq="sdf";
System.out.println(pq.getBytes());
a.sendHttpPost(pq.getBytes());
// System.out.print("发送数据完成"); a.recvHttpPost(88);
}
}
服务端控制台打印的结果是:
server is ok.
POST / HTTP/1.1
Client: POST / HTTP/1.1
Client: Cache-Control: no-cache
Client: Pragma: no-cache
Client: User-Agent: Java/1.6.0_10-rc2
Client: Host: 127.0.0.1:8088
Client: Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Client: Connection: keep-alive
Client: Content-type: application/x-www-form-urlencoded
Client: Content-Length: 3
Client: 我该如何得到想要的数据结果啊,并且给客户端相应的返回数据! 谢谢!!
import java.io.*;
import java.net.*;public class MyServer {
public static void main(String args[]) throws Exception{
ServerSocket serverSocket = new ServerSocket(8088);
System.out.println("server is ok.");
while(true){
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// DataInputStream in = new DataInputStream(socket.getInputStream());
String line = in.readLine();
//String line=in.toString();
System.out.println(line);
while(line!=null)
{
System.out.println("Client: " + line);
line = in.readLine();
// line=in.toString();
}
System.out.println("current user close the session.");
in.close();
socket.close();
}
}
}
客户端程序
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException; class HttpC{
private static String defaultURL = "http://127.0.0.1:8088";
private URL url;
HttpURLConnection hcon = null;
DataInputStream dis = null;
DataOutputStream dos = null;
String TAG = "HttpConnect";
public HttpC() throws MalformedURLException
{ url = new URL(defaultURL); if(url != null)
{
try
{
hcon = ( HttpURLConnection )url.openConnection(); //设置网络超时时间
hcon.setConnectTimeout(10000);
// 设置请求方法为POST
hcon.setRequestMethod("POST");
//设置输入输出流
hcon.setDoInput(true);
hcon.setDoOutput(true);
//Post请求不能使用缓存
hcon.setUseCaches(false);
hcon.setInstanceFollowRedirects(true);
//配置本次连接的Content-Type,配置为appplication/x-www-form-urlencoded
// hcon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
hcon.connect();
}
catch( Exception e )
{
hcon = null;
e.printStackTrace();
// GenUtil.WriteLog("HttpConnect", e.toString().getBytes());
}
}
}
public boolean sendHttpPost(byte[] data)
{
if ( hcon == null )
{
return false;
}
try {
dos = new DataOutputStream(hcon.getOutputStream());
dos.write(data, 0, data.length);
dos.flush();
// GenUtil.WriteLog("SendData:", data);
}//
catch( Exception e )
{
e.printStackTrace();
// GenUtil.WriteLog("sendHttpPost", e.toString().getBytes());
return false;
}
return true;
} public byte[] recvHttpPost(int recvLen)
{
int Templen = 0;
int actual = 0;
byte[] recv = null;
int Tlen = 0;
if ( hcon == null )
{
return null;
}
try{
dis = new DataInputStream( hcon.getInputStream());
Tlen = hcon.getHeaderFieldInt("Content-Length",0);
recv = new byte[Tlen];
if(Tlen > 0 )
{
while((Templen != Tlen) && (actual != -1))
{
actual = dis.read(recv,Templen, Tlen - Templen);
Templen += actual;
}
}
}
catch( Exception e )
{
e.printStackTrace();
// GenUtil.WriteLog("recvHttpPost", e.toString().getBytes());
return null;
}
// GenUtil.WriteLog("Recv Tlen:", GenUtil.PaddingHexintToString(Tlen,4).getBytes());
// GenUtil.WriteLog("RecvData:", recv);
return recv;
}
public void stop()
{
try {
if ( hcon != null ) hcon.disconnect();
if( dis != null ) dis.close();
if( dos != null ) dos.close();
hcon = null;
dis = null;
dos = null;
}
catch ( IOException ioe )
{
ioe.printStackTrace();
// GenUtil.WriteLog("stop", ioe.toString().getBytes());
}//结束try/catch };//结束try/catch/finally
}
}
public class HttpConnect{
public static void main(String args[]) throws MalformedURLException
{
HttpC a=new HttpC();
String pq="sdf";
System.out.println(pq.getBytes());
a.sendHttpPost(pq.getBytes());
// System.out.print("发送数据完成"); a.recvHttpPost(88);
}
}
服务端控制台打印的结果是:
server is ok.
POST / HTTP/1.1
Client: POST / HTTP/1.1
Client: Cache-Control: no-cache
Client: Pragma: no-cache
Client: User-Agent: Java/1.6.0_10-rc2
Client: Host: 127.0.0.1:8088
Client: Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Client: Connection: keep-alive
Client: Content-type: application/x-www-form-urlencoded
Client: Content-Length: 3
Client: 我该如何得到想要的数据结果啊,并且给客户端相应的返回数据! 谢谢!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货