我正在使用套接字連接我的客戶端與服務器,我需要一種方式,以便當有人試圖使用某個帳戶在客戶端上登錄時,它會將用戶名和密碼發送給服務器,並檢查該帳戶是否存在。我只需要知道如何在按登錄時將消息發送到服務器。從客戶端發送到服務器的Java消息
我想這使它將消息發送到服務器
public static void sendmsg(String a, String b)
{
try
{
String host = "127.0.0.1";
int port = 43655;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String sendMessage = a;
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
我們需要更多信息。你已經嘗試過一些東西嗎?如果是這樣,你卡在哪裏? – Mekap 2014-10-09 21:03:31
那麼我試着做一個方法,我可以發送消息到服務器這樣 - public void msgServer(String a,String b){但我無法弄清楚如何使它發送消息到服務器。 – dumbnerd111 2014-10-09 21:05:51
好的我編輯了問題 – dumbnerd111 2014-10-09 21:10:14