javatcp返回的简单介绍
本篇文章给大家谈谈javatcp返回,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、用java程序向linux,windows 发送命令查询 udp 和tcp 类型的端口是否可用 并返回有效结果
- 2、java如何通过tcp向指定的IP发送指令并获得返回的包?
- 3、java方面的, 应用TCP编程, 完成一个多线程版的服务端 ,当Client访问时 ,返回当前访问的时间。。求编码
用java程序向linux,windows 发送命令查询 udp 和tcp 类型的端口是否可用 并返回有效结果
可以在java中使用HttpClient来发起一个请求到windows服务器。
1. GET 方式传递参数
//先将参数放入List,再对参数进行URL编码
ListBasicNameValuePair params = new LinkedListBasicNameValuePair();
params.add(new BasicNameValuePair("param1", "数据")); //增加参数1
params.add(new BasicNameValuePair("param2", "value2"));//增加参数2
String param = URLEncodedUtils.format(params, "UTF-8");//对参数编码
String baseUrl = "服务器接口完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//将URL与参数拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //发起GET请求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式传递参数
//和GET方式一样,先将参数放入List
params = new LinkedListBasicNameValuePair();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加参数1
params.add(new BasicNameValuePair("param2", "第二个参数"));//增加参数2
try {
HttpPost postMethod = new HttpPost(baseUrl);//创建一个post请求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //执行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
java如何通过tcp向指定的IP发送指令并获得返回的包?
以下是一个展示java使用tcp通讯的简单例子,包括服务器和客户端代码:
/**
*TCPServer
*/
import java.io.*;
import java.net.*;
class TCPServer{
public static void main(String[] args)throws IOException{
ServerSocket listen = new ServerSocket(5050);
Socket server = listen.accept();
InputStream in = server.getInputStream();
OutputStream out = server.getOutputStream();
char c = (char)in.read();
System.out.println("收到:" + c);
out.write('s');
out.close();
in.close();
server.close();
listen.close();
}
}
/**
*TCPClient
*/
import java.io.*;
import java.net.*;
class TCPClient{
public static void main(String[] args)throws IOException{
Socket client = new Socket("127.0.0.1" , 5050);
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
out.write('c');
char c = (char)in.read();
System.out.println("收到:" + c);
out.close();
in.close();
client.close();
}
}
java方面的, 应用TCP编程, 完成一个多线程版的服务端 ,当Client访问时 ,返回当前访问的时间。。求编码
这个需要多线程么?把服务端搞成常驻内存的就好了吧。比如while。下面是我刚给你写的测试代码,仅供参考:
客户端:
static Socket server;
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getByName("134.132.207.228");
server = new Socket(address, 4567);
PrintWriter out = new PrintWriter(server.getOutputStream());
String str = "发送内容";
System.out.println("client send:"+str);
out.println(str);
out.flush();
BufferedReader wt = new BufferedReader(new InputStreamReader(server.getInputStream()));
str = wt.readLine();
System.out.println("client receive:"+str);
wt.close();
out.close();
server.close();
}
服务端:
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(4567);
while(true){
Socket client = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
System.out.println("server receive:"+str);
PrintWriter out = new PrintWriter(client.getOutputStream());
str = "来自服务器:"+getCurrentDateTimeStr();
out.println(str);
out.flush();
System.out.println(str);
out.close();
in.close();
client.close();
}
}
public static String getCurrentDateTimeStr(){
Calendar cal = Calendar.getInstance();
return getFormatedTime(cal.getTime());
}
public static String getFormatedTime(Date date) {
return dateFormat.format(date);
}
关于javatcp返回和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。