java代码写客户端,java客户端编程

java写tcp客户端测试类该怎么写

1.TCP服务端的程序编写

成都创新互联是一家集网站建设,夷陵企业网站建设,夷陵品牌网站建设,网站定制,夷陵网站建设报价,网络营销,网络优化,夷陵网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

package test;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.ServerSocket;

import java.net.Socket;

public class MyTcp{

private BufferedReader reader;

private ServerSocket serverSocket;

private Socket socket;

/**

* 创建服务端的程序,读取客户端传来的数据

*/

void getserver(){

try {

serverSocket = new ServerSocket(8998); //实例化服务端socket

System.out.println("服务器套接字已经创建成功");

while (true) {

System.out.println("等待客户机的连接:");

socket = serverSocket.accept(); //实例化socket对象

reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); //实例化BufferReader对象

getClientMessage();

}

} catch (Exception e) {

e.printStackTrace();

}

}

private void getClientMessage() {

try {

while (true) {

System.out.println("客户机传来的信息是:"+reader.readLine());

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

MyTcp myTcp = new MyTcp(); //创建本类对象

myTcp.getserver();

}

}

2.TCP客户端程序编写

package test;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.PrintWriter;

import java.net.Socket;

import java.nio.channels.WritableByteChannel;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.border.BevelBorder;

public class MyTcpClient extends JFrame{

private PrintWriter printWriter;

Socket socket;

private JTextField jTextField = new JTextField();

private JTextArea jTextArea = new JTextArea();

Container container;

/**

* 创建的Tcp客户端程序

*/

public MyTcpClient (String title) {

super(title);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

container = this.getContentPane();

final JScrollPane jScrollPane = new JScrollPane();

jScrollPane.setBorder(new BevelBorder(BevelBorder.RAISED)); //显示边框

getContentPane().add(jScrollPane,BorderLayout.CENTER);

jScrollPane.setViewportView(jTextArea);

container.add(jTextField,"South"); //将文本框放在窗体下面

jTextField.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

printWriter.println(jTextField.getText()); //将文本框的信息写入流(为下面的输出流写入信息做准备)

jTextArea.append(jTextField.getText() + "\n");

jTextArea.setSelectionEnd(jTextArea.getText().length());

jTextField.setText(null);

}

});

}

private void connect() {

jTextArea.append("尝试连接中...\n");

try {

socket = new Socket("127.0.0.1",8998);

printWriter = new PrintWriter(socket.getOutputStream(),true); //将printwriter中的信息流写入到套接字的输出流传送给服务端

jTextArea.setText("已完成连接\n\n");

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

MyTcpClient myTcpClient = new MyTcpClient("向服务器发送数据");

myTcpClient.setSize(500,200);

myTcpClient.setVisible(true);

myTcpClient.connect();

}

}

3.效果展示

1先将服务端的程序跑起来

2再将客户端的程序跑起来

3.客户端和服务端进行交互

webservice中怎样用java写客户端程序()

基于AXIS的web service:

1 比如要建一个Server.java类的web service

public class Server {

public String printInfo(String name){

return "Hello,"+name;

}

}

2 把Server.java改为Server.Jws放到

…\Tomcat 5.5\webapps\axis中,重启服务器

3 访问

4 在cmd中输入 cd D:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\axis\WEB-INF

输入命令:Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java

5 找到…\Tomcat 5.5\webapps\axis\WEB-INF下生成的localhost文件夹复制到工程中

6 建一个Client端的类进行测试:

public class Client {

public static void main(String arg[]) throws ServiceException, RemoteException{

ServerService ss=new ServerServiceLocator();

Server s=ss.getServer();

System.out.println("............"+s.printInfo("shiyou"));

}

}

怎么用java编写简单客户端程序

我这里有一个例子希望能够帮助你

public class Client{

private Socket socket;

try{

Socket socket=new Socket ("localhoast',8088);

ip=InetAddress.getLocalHost();

String localip=ip.getHostAddress();

System.out.println(localip);

String name=ip.getHostName();

System.out.println(name);

}

catch(Exception e){

}

public void start(){

try{

Run r2=new Run();

Thread t1=new Thread(r2);

t1.setDaemon(true);

t1.start();

Scanner sc=new Scanner(System.in);

OutputStream os=socket.getOutputStream();

OutputStreamWriter osw=new OutputStreamWriter(os);

PrintWriter writer=new PrintWriter(osw,true);

while(true){

writer.println(sc.nextLine());

}

}catch(Exception e){

}

}

public static void main(String args[]){

Client client=new Client();

client.start();

}

class Run implements Runnable{

public void run() {

while(true){

try {

InputStream is=socket.getInputStream();

InputStreamReader isr=new InputStreamReader(is);

BufferedReader br=new BufferedReader(isr);

while(true){

String str=br.readLine();

System.out.println("服务器说:"+str);

if("bye".equals(str)){

System.out.println("再见客户端");

System.out.println("聊天结束");

}

}

} catch (IOException e) {

e.printStackTrace();

} finally{

try {

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

}


网页名称:java代码写客户端,java客户端编程
网站地址:http://scyanting.com/article/hcojcc.html