关闭

Socket

785人阅读 评论(0) 收藏 举报

public   class   ServerTest   {
public   static   void   main(String[]   args)   throws   Exception   {
ServerSocket   ss   =   new   ServerSocket(9919);
Socket   sock   =   ss.accept();
ObjectOutputStream   out   =   new   ObjectOutputStream(sock.getOutputStream());
out.writeObject("test");
sock.close();
}
}
public   class   ClientTest   {
public   static   void   main(String[]   args)   throws   Exception   {
Socket   sock   =   new   Socket("localhost",   9919);
ObjectInputStream   in   =   new   ObjectInputStream(sock.getInputStream());
Object   obj   =   in.readObject();
System.out.println((String)   obj);
sock.close();
}
}
============================
import java.net.*;
import java.io.*;

public class XmlClient{
static Socket s;

public static void main(String[] args)throws Exception{
s = new Socket(InetAddress.getLocalHost(),5678);
PrintWriter pw = new PrintWriter(new OutputStreamWriter (s.getOutputStream()),true);
File f = new File("h://tij//book.xml");
BufferedReader br = new BufferedReader(new FileReader(f));

String str=null;
while((str = br.readLine()) != null)
pw.println(str);
pw.flush();

s.close();
}
}

 

import java.io.*;
import java.net.*;

public class XmlServer {

public static void main(String[] args) throws IOException{
ServerSocket server=new ServerSocket(5678);
Socket client=server.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("h://book.xml",true)));

String str=null;
while((str = br.readLine()) != null)
out.println(str);
out.flush();
client.close();
}

}

====================
如何实现socket通讯传输字符串功能??

--------------------------------------------------------------------------------


请问如何使下列代码实现传输字符串功能

server端:

import java.io.*;
import java.net.*;

public class SocketServer {
ServerSocket ss=null;
Socket s=null;
DataInputStream inStream=null;
DataOutputStream outStream=null;

public SocketServer() {
try{
init();
}
catch(Exception e){
System.out.println(e.toString());
}
}

void init() throws Exception{
ss=new ServerSocket(10001);
s.setSoTimeout(3000);
}

void waitForClient(){
try{
s=ss.accept();
inStream=new DataInputStream(s.getInputStream());
outStream=new DataOutputStream(s.getOutputStream());
outStream.writeUTF("1");
s.setSoTimeout(3000);
waitData();
}
catch(Exception e){
System.out.println(e.toString());
}
}

void waitData(){
while(true){
try{
String str=inStream.readUTF();
System.out.println("Server accept: "+str);
int nu=Integer.parseInt(str)+1;
if(nu>20){
System.out.println("Send end!");
break;
}
else{
str=Integer.toString(nu);
outStream.writeUTF(str);
}
}
catch(Exception e){
System.out.println(e.toString());
break;
}
}
}

public static void main(String[] args) {
SocketServer socketServer1 = new SocketServer();
socketServer1.waitForClient();
}
}
client端:

public void SocketClient_01() {
try{
init();
waitData();
}
catch(Exception e){
System.out.println(e.toString());
}
}

void init() throws Exception{
System.out.print("Please enter server ip address:");
String ip = new String(stdin.readLine());
s=new Socket(ip,10001);
inStream=new DataInputStream(s.getInputStream());
outStream=new DataOutputStream(s.getOutputStream());
s.setSoTimeout(3000);
}

void waitData(){
while(true){
try{
String str=inStream.readUTF();
System.out.println("Client accept: "+str);
str=Integer.toString(Integer.parseInt(str)+1);
outStream.writeUTF(str);
}
catch(Exception e){
System.out.println(e.toString());
break;
}
}
}

0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8628次
    • 积分:172
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:2篇
    • 译文:0篇
    • 评论:9条
    最新评论