套接字的问题 [英] problem with socket

查看:71
本文介绍了套接字的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这是我的程序......由此需要一次又一次地改变我的端口.....


你能帮我正确关闭插座......


代码:


<%!

ServerSocket MyServer;

套接字Mysocket;

文件文件=新文件(" file.txt");

FileInputStream fis = null;

BufferedInputStream bis = null;

DataInputStream dis = null;

DataOutputStream dos = null;

%>


<%

int port = Integer.parseInt(request.getParameter(" port1"));

try {

MyServer = new ServerSocket(port);

Mysocket = MyServer.accept();

fis = new FileInputStream(file );

bis = new BufferedInputStream(fis);

dis = new DataInputStream(bis);

dos = new DataOutputStream(Mysocket.getOutputStream());

while(dis.available()!= 0)

{

dos.writeBytes(dis.readLine());

}


fis.close();

bis.close();

dis.close();

dos.flush();

dos。 close();

Mysocket.close();

MyServer.close();

}

catch (例外e)

{


System.out.println(遇到错误!!!!+ e);

}


%>


代码结束:

hi all,
this is my program ...... By this need to change my port again and again .....

will you help me to proper closing of socket......

code:

<%!
ServerSocket MyServer;
Socket Mysocket;
File file = new File("file.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
DataOutputStream dos=null;
%>

<%
int port = Integer.parseInt(request.getParameter("port1"));
try{
MyServer = new ServerSocket(port);
Mysocket = MyServer.accept();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
dos = new DataOutputStream(Mysocket.getOutputStream());
while (dis.available() != 0)
{
dos.writeBytes(dis.readLine());
}


fis.close();
bis.close();
dis.close();
dos.flush();
dos.close();
Mysocket.close();
MyServer.close();
}
catch(Exception e)
{

System.out.println("Encountered an error!!!!"+e);
}

%>

End of code:

推荐答案

我不确定你想做什么?

你需要相同的ServerSocket来处理多个客户端 - 如果是这样你在循环中有了ServerSocket.accept()方法 - 因为客户端连接你创建一个新的线程来处理与客户端的通信,例如
I am not sure what you want do?
do you require the same ServerSocket to handle multiple clients - if so you have the ServerSocket.accept() method in a loop - as clients connect you to create a new Thread to handle communications with the client, e.g.
展开 | 选择 | Wrap | 行号



我不确定你想做什么?

你需要相同的ServerSocket来处理多个客户端 - 如果是这样你在循环中有了ServerSocket.accept()方法 - 因为客户端连接你创建一个新的线程来处理与客户端的通信,例如
I am not sure what you want do?
do you require the same ServerSocket to handle multiple clients - if so you have the ServerSocket.accept() method in a loop - as clients connect you to create a new Thread to handle communications with the client, e.g.
展开 | 选择 | Wrap | 行号



hi,

当我尝试上面的代码时,它完全关闭了tomcat。我要重启tomcat。我把套接字编程放在javascript中。

我没有多个客户端。我必须将数据发送到一个端口以及从其他端口接收数据。

它用于接收数据以及向它们提供一次数据。但我的问题实际上是一旦我用于传输数据,我就无法再次使用相同的端口。我不得不再次更改端口号以进一步传输数据。


比Q

-raju
hi ,
when i tried the above code it shutdown tomcat totally. I have to restart tomcat. I had put the socket programing in javascript.
And i dont have multiple clients. I have to send the data to one port as well as receive the data from other port.
It is working for receiving data as well as giving data to them for once. But my problem is actually I can''t able to use same port again once i had used for transfer data. I had to change the port number again for further trasfer of data.

thanQ
-raju



很奇怪你不能使用同一个端口。

我将一个单线程TCP服务器放入循环中。它打开了一个ServerSocket,接受了一个客户端,聊了聊,然后关闭了ServerSocket。然后使用相同的端口号等等再次循环并打开ServerSocket。没问题!


这是Tomcat / Javascript问题吗?

very strange you cannot use the same port.
I put one of my single threaded TCP servers into a loop. It opened a ServerSocket, accepted a client, talked and then closed the ServerSocket. It then looped back and opened the ServerSocket again using the same port number, etc etc. No problem!

Is it a Tomcat/Javascript problem?


这篇关于套接字的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆