c# - Server start method throws exception, port already in use? -
i creating server-client application server , client can talk each other. when call start method on server, error saying this:
only 1 usage of each socket address (protocol/network address/port) permitted
here program.cs:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.net; using system.net.sockets; namespace networkingtest { class program { static void main(string[] args) { bool readline = true; string input = ""; while (true) { if (readline == true) { input = console.readline(); } if (input == "server") { server server = new server(ipaddress.any, 12346); readline = false; } if (input == "client") { client client = new client(ipaddress.parse("myipv4"), 12346); readline = false; } } } } }
here server.cs:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.net; using system.net.sockets; using system.threading; namespace networkingtest { class server { public tcplistener server; public server (ipaddress ip, int port) { server = new tcplistener(ip, port); server.start(); thread serverrunthread = new thread(new threadstart(runserver)); serverrunthread.start(); } void runserver () { while (true) { tcpclient client = server.accepttcpclient(); console.writeline("client connected!"); thread serverhandlerthread = new thread(new parameterizedthreadstart(handleclient)); serverhandlerthread.start(client); } } void handleclient(object c) { tcpclient client = (tcpclient)c; networkstream stream = client.getstream(); int i; string data = null; byte[] bytes = new byte[256]; while((i = stream.read(bytes, 0, bytes.length)) != 0) { data = encoding.ascii.getstring(bytes, 0, i); console.writeline("received: " + data); } stream.close(); client.close(); } } }
and here client.cs:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.net; using system.net.sockets; using system.threading; namespace networkingtest { class client { public tcpclient client; public client (ipaddress ip, int port) { client = new tcpclient(); client.connect(ip, port); thread thread = new thread(new threadstart(connecttoserver)); thread.start(); } void connecttoserver () { while (true) { string input = console.readline(); if (input == "exit" || input == "quit" || input == "close") { break; } else { sendmessagetoserver(input); } } } void sendmessagetoserver (string message) { networkstream stream = client.getstream(); byte[] bytes = encoding.ascii.getbytes(message); stream.write(bytes, 0, bytes.length); stream.close(); } } }
edit: there problem in program.cs, fixed while loop:
while (readline == true) { input = console.readline(); if (input == "server") { server server = new server(ipaddress.any, 12346); readline = false; } if (input == "client") { client client = new client(ipaddress.parse("10.0.0.5"), 12346); readline = false; } }
that happens because of error in while(true)
loop. first ask user line console.readline()
. if input
"server" start server, go beginning of while loop , readline
variable false, , input
still "server", creates second server (basically have infinite loop). since have 1 sever on port - second try fails error see.
to fix, remove while loop, or more correctly, this:
static void main(string[] args) { while (true) { console.writeline("type \"server\" start server, type \"client\" start client, type \"exit\" exit"); string input = console.readline(); if (input == "server") { server server = new server(ipaddress.any, 12346); } else if (input == "client") { client client = new client(ipaddress.parse("myipv4"), 12346); } else if (input == "exit") return; } }
Comments
Post a Comment