java方法的单独_java - 如何控制从一个单独的方法PrintWriter的? - SO中文参考 - www.soinside.com...

进出口工作建设,从平板会谈我的电脑我自己的GUI程序。我已经在服务器端在Java中这样做,但我的问题是在客户端。

我想从一个单独的方法将数据发送出的PrintWriter到服务器。我已经在下面的代码来完成的发送(发送“A”),但我不能找出如何从一个单独的方法发送。我相信它,即时通讯不理解一个基本的Java范围的问题。我会很感激的帮助。

我曾尝试变量移动到其他领域。

import java.io.*;

import java.net.*;

public class TestClient {

public static void main(String[] args) {

String hostName = "192.168.0.3";

int portNumber = 6666;

try ( //Connect to server on chosen port.

Socket connectedSocket = new Socket(hostName, portNumber);

//Create a printWriter so send data to server.

PrintWriter dataOut = new PrintWriter(connectedSocket.getOutputStream(), true);

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))

) {

//Send data to server.

dataOut.println("a");

}catch (UnknownHostException e) {

System.err.println("Don't know about host " + hostName);

System.exit(1);

} catch (IOException e) {

System.err.println("Couldn't get I/O for the connection to " +

hostName);

System.exit(1);

}

}

public static void sendToServer() {

//I want to control the print writer from this method.

//I have failed i all the ways i have tried.

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值