3
Answers

Client Server

Ahmad

Ahmad

16y
2.2k
1
Hi everybode iam making a client server program when i request a connect to the server the client give me an Exception that the computer refuse it here is the code of the client program : ************************************************************* Socket scc; private void Connection_btn_Click(object sender, EventArgs e) { String tip = Ip_txt.Text; String tport = Port_txt.Text; scc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(tip); int port = Int32.Parse(tport); IPEndPoint endpoint = new IPEndPoint(ip, port); scc.Connect(endpoint); if (scc.Connected) { statusStrip1.Text = "Connected"; } } in the server code i put a thread in the form load to start when the form is loaded here is the thread code (a thread job is to listen on the port) : public void listen() { //MessageBox.Show("inside listen"); sc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Check why it is true********************* sc.Blocking = false; IPAddress ip = IPAddress.Parse(ipText.Text); int port = Int32.Parse(portText.Text); IPEndPoint endpoint = new IPEndPoint(ip, port); sc.Bind(endpoint); while (true) { //MessageBox.Show("before listen"); // MessageBox.Show("After listen"); if (sc.Connected) { sc.Listen(10); client = sc.Accept(); //clientsock = serversocket.Accept(); MessageBox.Show("Accepted"); client = sc.Accept(); if (client == null) { MessageBox.Show("Winsock error: " + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error())); this.Close(); } if (client.Connected) { host = (IPEndPoint)client.LocalEndPoint; comboBox1.Items[counter] = host.Address.ToString(); counter++; tc = new Thread(new ThreadStart(makethread)); tc.Start(); } } } } Note: the ip that i put it in the textbox is 127.0.0.1 and the port 5555 in both the server and client.
Answers (3)